I have a website done in Wordpress and I need to make some changes in the fiendly URLs.
I’ve created a page from the admin panel named detail, this page reads the template file detail.php from the templates folder.
The URL that is currently mounted is http://www.domain.com/detail/1234/ and I need that it could be accessed as http://www.domain.com/anything/1234/.
The following lines have been generated by Wordpress but I don’t understand them and I don’t know how to modify them for my purpose:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
First you should really understand what those rules are doing and what you really want to achieve. Then you can try to change the system to fit your needs.
IfModule ensures everything inside is processed only when mod_rewrite Apache module is present. All the other directives are from this module.
RewriteEngine On enables URL rewriting.
RewriteBase / tells the engine that the rules operate inside root. See also the general question on how RewriteBase works.
RewriteRule ^index\.php$ - [L] means that no more rules should be processed ([L]) if the current URL to be rewritten is index.php. No rewrite takes place. RewriteRule directive accepts a regex. See also regex tag here on SO.
All RewriteCond directives apply to the following RewriteRule. Unless [OR] flag is added, they must be all satisfied at the same time to execute the rule. In this case they mean:
Requested resource is not a regular file.
Requested resource is not a directory.
Rewrite any (at least one character long) URL to index.php. This is the last ([L]) rule to be processed.
When adding new RewriteRules, you probably want to use the WordPress way of doing this, as described in Zac’s answer. Figuring out the right rule by analogy to the many examples in the manual or here on SO should not be hard.
Put into functions.php maybe a better idea:
functions.php
function setRewriteRule($orgRules){
return array( '/([^/]+)/([0-9]+)/?' => 'index.php?post=$matches[1]' ) + $orgRules;
}
add_filter('rewrite_rules_array', 'setRewriteRule');
Then you just need flush the rewrite rules, I usually use 'rewrite-rules-inspector' plugin.
This should solve your problem give it a try ... you can write your own custom permalink without adding any code also if someone tries to access the page via old URL they will be redirected to the new one.
WordPress Custom Permalinks
After installing this you just have to go into pages and type your own URL below the heading
Related
I'm writing a CMS in PHP, and now I'm working at the themes feature. I have a .htaccess file:
RewriteEngine ON
RewriteRule ^([a-zA-Z][a-zA-Z0-9]*)$ index.php?m=$1
RewriteRule ^([a-zA-Z][a-zA-Z0-9]*)/(.+)$ index.php?m=$1&p=$2
If I have a request to:
/page
it must load the view function of the class called page.
If I have a request to:
/page/test
it must load the view function of the class called page, with the parameter 'test'.
This all works,
But I want it to apply the RewriteRules ONLY if the request does NOT start with:
/THEMES/
So I can apply CSS styles etc...
Can anyone help me? Thanks.
Add this before your rewrite rules:
RewriteCond %{REQUEST_URI} !^/THEMES/
You could use an additional rule to stop the rewriting process:
RewriteRule ^THEMES/ - [L]
RewriteRule ^([a-zA-Z][a-zA-Z0-9]*)$ index.php?m=$1
RewriteRule ^([a-zA-Z][a-zA-Z0-9]*)/(.+)$ index.php?m=$1&p=$2
Do something like:
RewriteRule ^THEMES - [L]
That means: if the request starts with THEMES, just serve it.
Another possible solution is:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
That means: do not rewrite if the request resolves to an existing file (first line) or directory (second line).
Maybe you should read the documentations, is really well written.
If you want to include css/js files while using url-rewriting use that tag to specify the main url.
< base href="http://www.your-web-site-adress.com" >
Then you may easily include your css/js files like that:
< script src="/blabla.js" >
it'll add base href as prefix.
I have a Wordpress site that responds with JSON to every request, i.e. the following pages:
/
/about
/about/team
All respond with json. The htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I'd like to keep what I have now, but also respond with the same data when index.json is added to the url:
/index.json
/about/index.json
/about/team/index.json
How can I update my htaccess to also respond to these urls in the same way?
Based on what #MrWhite said in comments (gonna quote here, because comments might get removed later),
However, the "problem" here is WordPress. WP routes URLs based on the REQUEST_URI, which does not change during a URL rewrite. So you really need to implement this within WordPress itself.
you could probably just “reset” the value of $_SERVER['REQUEST_URI'] in PHP, if it ends with /index.json. (Relatively easy string manipulation / regex job.)
The remaining problem would just be to figure out a “correct” way to do this – I guess trying to do this via any hooks from inside the theme’s functions.php or a plugin, might be too late. You can give that a try anyway, if you like - the order of hooks as described here is what I would go by, https://wordpress.stackexchange.com/a/162869
The init hook is pretty far down, even after the current user was determined, etc. – but maybe plugins_loaded or setup_theme might be suitable for stuff like this.
If that doesn’t work, still some possible alternatives:
If you can influence the PHP configuration, auto_prepend_file would allow you to specify a script that gets automatically run before anything else PHP does - so you could fix the value before the index.php code even executes.
You could write your own little “wrapper” file, say index2.php, that fixes the value, and then simply includes the original index.php afterwards. Only problem with that - you probably wouldn’t want to change the default WP rewriting between the BEGIN and END WORDPRESS comments in the .htaccess, because WP will overwrite that part, when you flush your permalink settings. But an additional internal rewrite of index.php to index2.php after, could probably solve that.
If mod_proxy is available, you could proxy the request internally. Probably not the best in terms of performance, but at least an alternative to an external rewrite, if you wanted to avoid that at all cost.
Not sure if this is the right section of Stackoverflow to ask my question...
But here it is:
So I am using the below on the .htaccess file:
RewriteEngine On
RewriteRule ^sale?$ /discount-page/
So that when people visit example.com/sale page, they see content from example.com/discount-page/
But when I visit example.com/sale it shows 404 error saying that the URL /discount-page is not available on this server...
Why is it happening?
Here's how my entire .htaccess file looks like:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Can anyone help please?
When using WordPress, you can't simply rewrite the URL in .htaccess to the %postname% (the real URL) since WP still looks at the REQUEST_URI in order to route the URL. Even though you are rewriting /sale to /discount-page/ (the actual URL), WordPress sees /sale (the requested URL) - which doesn't exist inside WP; hence the 404.
Although not your intention, you could change this to an external redirect to get around this problem (which also avoids a potential duplicate content issue). For example:
RewriteRule ^sale$ /discount-page/ [R,L]
(I removed the ? in ^sale?$, as that does look erroneous. Or do you really want to match /sale or /sal?)
Alternatively, you could try rewriting to the underlying "plain" permalink. ie. explicitly pass the %post_id%. This is different to rewriting to the %postname%, since WP shouldn't need to check the REQUEST_URI in order to route the URL. For example:
RewriteRule ^sale$ /index.php?p=123 [L]
Where 123 is the %post_id% of your discount-page. By rewriting directly to index.php, you are effectively bypassing WP's front-controller.
Note that this must go before the standard WordPress directives in .htaccess (aka the front-controller).
However, I still feel there should be a more WordPress-like way of doing this, which is why I initially suggested asking this question over on the WordPress Stack. However, if you do that, don't mention ".htaccess". What you are really creating is a URL alias or something like that. For example: Have two different URLs show the homepage
Long version (you can skip to TL;DR if you want to):
I am working with a Wordpress site that was set up by someone else. The website has multiple pages where page has tabbed content which is accessible through #. For eg:
www.example.com/services/category1/#tab-service1
www.example.com/services/category1/#tab-service2
www.example.com/services/category2/#tab-service1
www.example.com/services/category2/#tab-service2
www.example.com/services/category2/#tab-service3
Now, when search engines index they are indexing only www.example.com/services/category1/ and www.example.com/services/category2/. This creates a problem where we cannot have search engines point directly to the content within a given tab. What we want is for search engines to show links that takes users directly to (say) www.example.com/services/category2/#tab-service3.
Now, I don't think google can index such # based content on its own. So, I am thinking of using apache rewrites to try to resolve this issue. I have access to .htaccess file only (from a config perspective).
TL;DR
How to redirect www.example.com/services/category1/service3/ to www.example.com/services/category1/#tab-service3 using apache redirects (I have access to .htaccess file)?
This is what I am trying but it's not working:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/services/category1/([a-z0-9])/? [NC]
RewriteRule .* /services/category1#tab-%1 [R,NE,L]
Someone also adviced to look into pushState server config to fix this. I am not sure how to use pushState.
UPDATE:
I have updated the rewrites to the following but it still doesn't work. It keeps showing Wordpress' 404 page
<IfModule mod_rewrite.c>
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /domainfolder/
RewriteCond %{REQUEST_URI} ^/services/category1/([a-z0-9]+)/?$ [NC]
RewriteRule ^/services/category1/([a-z0-9]+)/?$ /services/category1/#$1 [NE,R,L]
</IfModule>
Your %{REQUEST_URI} regex is wrong. The pattern ^/services/
category1/([a-z0- 9 ])/? matches /services/category1/{any 1 char of a-z or 0-9} format followed by an optional slash. So this does not match your request /services/category1/service3 but matches /services/category1/a/ .
You should be using
^/services/category1/([a-z0-9]+)/?$
I am trying to create 'faux' pages that all link to the homepage with associated variables in the GET data. For example:
http://example.com/img/IMGNAME/comment/5678/
provide data like:
http://example.com/?image=IMGNAME&comms=5678
I have the following in my htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^img/([^/]+)/comment/([^/]+) /?image=$1&comms=$2 [L,NS]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
the issue is that i have a post that has a slug of IMGNAME (this is how im finding the post given the GET variable on the homepage).
When I manually enter http://example.com/?image=IMGNAME&comms=5678 it all works great.
When I enter http://example.com/img/IMGNAME/comment/5678/
I am redirected to http://example.com/category/IMGNAME/ (the url address changes AND it renders the single.php template)
I am assuming index.php (or '/') then gets processed by wordpress, in which it attempts to find the post with the closest name to the content of the variable im handing it and REDIRECTS to the post?
Does anyone know how to have the entered url STAY in the address bar and allow the homepage to process the GET variables?
Do i need to create a different version of the base index.php file to have it not redirect/search for similarly named posts? I still need it to process my theme template pages with wordpress tags etc.
I think rather then doing the rewriting in .htaccess you should add new custom rewrite rule wordpress way.
function my_custom_rewrites(){
add_rewrite_rule(
'img/(.*)/comment/(.*)',
'index.php?image=$matches[1]&comms=$matches[2]',
'top' );
}
add_action( 'init', 'my_custom_rewrites' );
You can also try the tips shared here http://docs.dev4press.com/tutorial/practical/debug-wordpress-rewrite-rules-matching/ that might help you understand which Rewrite rule is matching the url and why a particular template is being rendered.
Your URL might very well be matching with other patterns of rewrite rules of Wordpress. So registering your own rule in wordpress gives you the opportunity to get it checked before the core rules, Notice the third parameter of the add_rewrite_rule which says 'top', this specify the same