I'm trying to rewrite my URLs to be more SEO friendly, and I'm doing this in my .htaccess file using the following code:
RewriteEngine On
RewriteRule blog/(.*)/$ blog/index.php?&link=$1 [NC]
RewriteRule ^/*(.+/)?([^.]*[^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
That code allows me to rewrite http://example.com/blog/index.php?link=22 into http://example.com/blog/page-title/
The only problem here, is that I'm trying to get it to work without the ending slash (/). I've tried removing it in the third line of the .htaccess code (right fater the $2), but that just breaks everything and the rewrite doesn't work at all.
Any help is greatly appreciated.
Try now, both with or without slash should work.
RewriteEngine On
RewriteRule blog/(.*)/?$ blog/index.php?&link=$1 [NC]
RewriteRule ^/*(.+/)?([^.]*[^/])$ http://%{HTTP_HOST}/$1$2 [L,R=301]
Related
I have a multilingual wordpress website and want to redirect website of given region to given language,
xyz.de --> xyz.de/de/
xyz.co.uk --> xyz.co.uk/en/
direct access to xyz.de/de and xyz.co.uk/en are working properly. So there is no problem on wordpress side.
Now, I am trying to change the htaccess file of xyz.de and xyz.co.uk so that they redirect the website.
Considering xyz.co.uk
I want to add a RewriteCond such that whenever there is no /en trailing after xyz.co.uk it will automatically add /en.
For example xyz.co.uk/<trailing address> results in xyz.co.uk/en/<trailing address>
So far I have the following code, which somehow doesn't seem to work,
RewriteCond %{REQUEST_URI} !^/en
RewriteRule ^(.*)$ http://xyz.co.uk/en/$1 [L]
The negation of /en is not working! I have also tried
RewriteCond %{REQUEST_URI} !/en
RewriteRule ^(.*)$ http://xyz.co.uk/en/$1 [L]
Could someone tell me where I am going wrong? seems like I have gone wrong in writing RegEx and suggest if there is better way to achieve the same, that does not affect the SEO across different domains.
Use THE_REQUEST variable instead of REQUEST_URI:
RewriteCond %{HTTP_HOST} \.co\.uk$ [NC]
RewriteCond %{THE_REQUEST} !/en/ [NC]
RewriteRule ^ /en%{REQUEST_URI} [L,R=302,NE]
Make sure to keep this rule as your very first rule in .htaccess.
Change it to R=301 once you've tested.
Disclaimer : I dont know anything about URL rewriting
currently my URL looks like
legalHQWithNewAddressTable/legalHQ/public/admin/Parties.php?caseid=7 (any number basically)
But how can I make it to looks like the following
legalHQWithNewAddressTable/legalHQ/public/admin/Parties/caseid/7/
I'm trying but not working RewriteRule ^([a-zA-Z0-9]+)/?
Please dont make things complicated I dont know anything about URL rewriting so please keep it as simple as possible.
Any Idea?
Try adding these rules to the htaccess file in the /legalHQWithNewAddressTable/legalHQ/public/ folder:
RewriteEngine On
RewriteBase /legalHQWithNewAddressTable/legalHQ/public/
RewriteCond %{THE_REQUEST} /admin/([A-Za-z0-9]+)\.php\?caseid=([0-9]+)&?([^\ ]*)
RewriteRule ^ admin/%1/caseid/%2/?%3 [L,R]
RewriteRule ^admin/([A-Za-z0-9]+)/caseid/([0-9]+)/ admin/$1.php?caseid=$2 [L,QSA]
Place this rule in your /legalHQWithNewAddressTable/legalHQ/public/admin/.htaccess:
Options -MultiViews
RewriteEngine On
RewriteBase /legalHQWithNewAddressTable/legalHQ/public/admin/
RewriteRule ^Parties/caseid/([0-9]+)/?$ Parties.php?caseid=$1 [L,QSA,NC]
I'm attempting to get apache to rewrite a request to mysite.com/blog/<number> to the file /blog.php?page=<number>. However, using a regex which does match that path, the URL is not rewritten and instead a 404 is returned as /blog/ is a nonexistent directory.
Here is the RewriteRule I'm using:
RewriteEngine On
RewriteRule ^blog/[0-9]+/? /blog.php?page=$1 [L]
I haven't used apache in quite a while, so I may have forgotten some extremely simple item which is required, but I cannot see anything wrong with the rule.
Edit:
RewriteRules from httpd.conf:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
You're not grouping the numbers after ^blog/ using round brackets, that's why $1 will be empty.
Try this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^blog/([0-9]+)/?$ /blog.php?page=$1 [L,NC,QSA]
So, I've been working on htaccess since like yesterday. I have two Rewrite statements in my htaccess file.
RewriteRule ^.*wp-login\.php\?loggedout=true.*$ /not_found [R,L]
The above statement works. While
RewriteRule ^.*wp-login\.php\?action=login.*% /not_found [R,L]
...doesn't!
To make the second case work, I used the following statements.
RewriteCond %{QUERY_STRING} action=logout
RewriteRule ^wp-login\.php$ /not_found/? [R,L]
So, not using "?action=logout" in the RewriteRule in the second case seems to solve the problem.
Yes, the problem is solved, but I'd like to understand why. This is so puzzling.
Any help is appreciated. Thank you.
Your earlier rules are incorrect because QUERY_STRING cannot be matched in RewriteRule. RewriteRule only match request uri without query string.
So correct rules are:
RewriteCond %{QUERY_STRING} (^|&)action=logout(&|$)
RewriteRule ^wp-login\.php$ /not_found/? [R,L]
OR this to include both query parameters:
RewriteCond %{QUERY_STRING} (^|&)action=(login|logout)(&|$)
RewriteRule ^wp-login\.php$ /not_found/? [R,L]
I have begun learning htaccess and i'm massively struggling. I'm simply trying to remove my .php from a URL
subdomain.example.com/weddings.php
to
subdomain.example.com/weddings/
to be honest that's not entirely it. the full url is
subdomain.example.com/weddings.php?section=pyromusicals#divname
which im trying to make into
subdomain.example.com/weddings/divname
for now though just the removal of the .php to / would be helpful.
so far I have in my htaccess file
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(.*).php$
RewriteRule ^(.*)$ %1 [L,QSA]
which is re-writing to
subdomain.example.co.uk/wedding-fireworks/
which is great but it also generates a 404 error page not found.
The requested URL /wedding-fireworks/index was not found on this server.
Is this what you are wanting to do?
RewriteRule ^([^/]+)/([^/]+) /$1.php?section=pyromusicals#$2 [QSA,L]
RewriteRule ^([^/]+) /$1.php [QSA,L]
Note that is assuming your section will always = pyromusicals
If section changes, you will need to include it in your URL also example below.
http://www.yoursite.com/somephppage/nameofsection/divname
Then your mod rewrite would look like this.
RewriteRule ^([^/]+)/([^/]+)/([^/]+) /$1.php?section=$2#$3 [QSA,L]
RewriteRule ^([^/]+) /$1.php [QSA,L]