I am trying to rewrite my URLs to have clean URLs. For example I want the following URL rewritten like so:
www.domain.com/events/events.php
www.domain.com/events/
It is rewritting the URL correctly but the rewritten URL is giving a 404 error. What am I doing wrong?
I am using the following rule:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(.*)/(.*)\.php\ HTTP
RewriteRule ^ http://www.domain.com/%3/? [R=301,L]
Once you've got the rules to externally redirect the browser, you then need to add rules to internally rewrite to the actual files. So you need to add:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /$1/$1.php [L]
Now, this rule takes a URI that looks like /(something)/ to /(something)/(something).php. So:
/foo/ to /foo/foo.php
/example/ to /example/example.php
etc.
There's no other info you're providing in the rewritten URI that tells you what the php filename is, so if you have something like this:
/foo/bar.php
there's no way to extract the "bar" out of /foo/. You'd need to additionally encode that in the clean url:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(.*)/(.*)\.php\ HTTP
RewriteRule ^ http://www.domain.com/%2/%3/? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /$1/$2.php [L]
Related
URL is rewritten using following rule.
RewriteEngine on
RewriteRule ^([a-zA-Z]+)/$ category?id=$1
to change
http://localhost/newsite/category?id=home
to following structure
http://localhost/newsite/home/
Now I tried to redirect, newsite/category?id=home to newsite/home/, to make clean URL using redirect rule, such as 301, redirect, but it doesn't work.
You can use this set of rules in newsite/.htaccess:
RewriteEngine on
RewriteBase /newsite/
RewriteCond %{THE_REQUEST} /category(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/$ category.php?id=$1 [L,QSA]
You missed the .php in the rewrite statement
RewriteRule ^([a-zA-Z]+)/$ category?id=$1
change to
RewriteRule ^([a-zA-Z]+)/$ category.php?id=$1
So the problem I'm looking at and haven't managed to solve!
For example the url is http://someurl.com/brands/brand
What I want to accomplish is that htaccess lets te request go to url: http://someurl.com/brands/brand but removes the "brands" part from the url.
Also if the url: http://someurl.com/brands is called the page "brands" needs to be displayed. So only when there is a brand after /brands/ it needs to do a URL rewrite to http://someurl.com/brand
I have tried this but this piece does a redirect to the new location witch doesn't exist.
RewriteRule ^onze-merken/(.*)$ /$1 [L,R=301]
So I need the above with out the redirect, it only needs to rewrite the URL.
Any help would be greatly appreciated, Thanks!
This is now my htaccess part where the rewriting is done!
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /onze-merken/(\S+)\s [NC]
RewriteRule ^ /%1 [R=302,L,NE]
# Only rewrite if the directory doesn't exist.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# internal forward from pretty URL to actual one
RewriteRule ^((?!onze-merken/).+)$ /onze-merken/$1 [L,NC]
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
RewriteRule ^(uploads/([a-z0-9_\-\.]+)/__images/custom/(.*)/)(.*)$ http://someurl.com/uploads/$2/__images/custom/$3/$4 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /brands/(\S+)\s [NC]
RewriteRule ^ /%1 [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!brands/).+)$ brands/$1 [L,NC]
I need to remove the /design/ from this URL (and any that contain it).
https://www.domain.com/design/subcategory/productname1
At the end, I want it to look like this:
https://www.domain.com/subcategory/productname1
But since that would obvious cause a 404 of you just deleted that part of the URL, I need to populate that second URL with the contents of the first. I apologize if I am not using the correct terms but here is an example of something similar I have on my site.
In my header menu, I have a link that indicates it will take you here:
https://www.domain.com/index.php?route=product/search&tag=shirts
But I wanted it to look like this:
https://www.domain.com/shirts/
So I researched it and this solution worked great:
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]
Can something similar be done to remove the /design/ part of the URL?
Thanks!
Try these rules:
RewriteEngine On
# remove /design/ from URLs and redirect
RewriteCond %{THE_REQUEST} \s/+design/(\S*)\s [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# internally add design/ to know set of URIs
RewriteRule ^(skulls-bones|characters|abstract|animals|games|geek-nerd|graphic|keep-calm|movies-tv|pop-culture|tattoo|typography)(/.*)?$ design/$1$2 [L,NC]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/design/ [NC]
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]
I have URL rewrite rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/sites/
RewriteRule ^([^/]+)/?(.*)$ sites/$1/$2 [NC,L]
When this URL is called: http://example.com/suyash
It internally calls http://example.com/sites/suyash but keeps the URL as http://example.com/suyash only. Please note that the folder sites/suyash actually exists
The issue I am facing is that when I call a URL: http://example.com/Suyash (Uppercase) it shows a 404 error.
When I use the mod_speling module and add the following to the code:
CheckSpelling On
and now when I call http://example.com/Suyash it changes the URL to http://example.com/sites/suyash
How do I get it to only change URL from http://example.com/Suyash to http://example.com/suyash?
mod_speling will redirect the browser in order to fix spelling, there's no way to avoid this. The only thing I can think of is to try proxying the request internally by using the P flag. You'll need to make sure you have mod_proxy loaded. I haven't tested this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/sites/
RewriteRule ^([^/]+)/?(.*)$ sites/$1/$2 [NC,L,P]
I would say keep CheckSpelling off.
And then have your rule like this:
CheckSpelling Off
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/$0 -f [NC]
RewriteCond %{DOCUMENT_ROOT}/$0 -d [NC]
RewriteCond %{REQUEST_URI} !^/sites/ [NC]
RewriteRule ^([^/]+)/?(.*)$ sites/$1/$2 [L]
I have URLs in the following format:
http://www.mysite.com/index.php?l=accommodation/lodge/some-place
I would like to use htaccess to rewrite it to this format:
http://www.mysite.com/accommodation/lodge/some-place
How would I do that? I started trying to do it with drupal's htaccess file and got something like this:
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?l=$1 [L,QSA]
Problem is, if I enter the original index.php url, it still shows. It must redirect to the short version. In other words, this:
http://www.mysite.com/index.php?l=accommodation/lodge/some-place
Must forward to this:
http://www.mysite.com/accommodation/lodge/some-place
Thoughts?
Insert this additional rule above your rule:
RewriteCond %{THE_REQUEST} \s/+index\.php\?l=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
This will externally redirect http://www.mysite.com/index.php?l=accommodation/lodge/some-place to http://www.mysite.com/accommodation/lodge/some-place