Still having mod_rewrite problems / .htaccess - apache

I have several rewrite rules in my .htaccess file that all work fine and I want to add a specific case to turn
sinaesthesia.co.uk/category/psoriasis
into
sinaesthesia.co.uk/category.php5?category=psoriasis
So I tried:
RewriteRule ^(.)category/(.)$ /$1category.php5?category=$2 [L]
which doesn't work. I've tried it without capturing the stuff before 'category' because category.php5 is at the root anyway, so it ought to work without that, have tried:
RewriteRule ^category/([a-z]+?)$ /category.php5?category=$1 [L]
with and without capturing the stuff before category, and nothing works - in fact, I generally get a 500 error! Here is the rest of the file:
RewriteEngine On
#remember to change this to aromaclear
RewriteCond %{HTTP_HOST} !^sinaesthesia\.co.uk$ [NC]
RewriteRule ^(.*)$ http://sinaesthesia.co.uk/$1 [R=301,L]
#Translate default page to root
RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php5|html)\ HTTP
RewriteRule ^(.*)index\.(php5|html)$ /$1 [R=301,L]
#translate any .html ending into .php5
RewriteRule ^(.*)\.html$ /$1\.php5
#change / for ?
RewriteRule ^(.*)\.html/(.*)$ /$1\.html?$2
#strip .html from search res page
RewriteRule ^(.*)search/(.*)$ /$1search_results\.html/search=$2
#translate product details link from search res page
RewriteRule ^products/(.*)/(.*)/(.*)$ /product_details.php5?category=$1&title=$2&id=$3 [L]
#Translate products/psorisis/chamomile-skin-cream-P[x] to productview.php5?id=1
RewriteRule ^products/.*-P([0-9]+) /productview.php5?id=$1 [L]

Ah: because I have a document called category.php5 and I'm trying to use category/psoriasis, the server tries to resolve that as category.php5/psoriasis, which fails. Fixed it now!

Related

how to Rewrite two parameters URL to SEO friendly URL and Redirect to SEO friendly url

This is my page url example.com/platform/bidProject.php?pID=JCVGK&name=Proof%20Reading%20Blogs
These rules helped me to Rewrite this as example.com/platform/project-bids/JCVGK/Proof-Reading-Blogs/
RewriteRule ^platform/project-bids/(.*)/(.*)/?$ /platform/bidProject.php?pID=$1&name=$2 [L,NC]
RewriteCond %{THE_REQUEST} \s/platform/bidProject.php?pID=$1&name=$2
RewriteRule ^platform/bidProject.php?pID=$1&name=$2 /platform/project-bids/(.*)/(.*)/ [NC,R=301,L]
but the issue is if I visit to the page example.com/platform/bidProject.php?pID=JCVGK&name=Proof%20Reading%20Blogs the url stays same. I want this to be redirected to example.com/platform/project-bids/JCVGK/Proof-Reading-Blogs/
So I tired this:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^(\S+)$ /$1 [NE,R=302,L]
RewriteRule ^([^/]+)/([^/]+)/?$ /platform/bidProject.php?pID=$1&v=$2 [L,QSA]
Seems I am doing something wrong with this because it makes no any affect on this.
I found a solution to redirect using JavaScript but I like to have .htaccess solution because JavaScript can be disable and can be seen in the source code. The basic intention for doing this fails here.
How can I achieve this using .htaccess
My htaccess path is example.com/.htacsess
You can use these Rules
RewriteEngine on
#redirects /platform/bidProject.php. php?pid=val1&name=val2 to /platform/project-bids/val1/val2/
#redirects the old url to the new one
RewriteCond %{THE_REQUEST} \s/platform/bidProject.php\?pID=([^&]+)&name=([^&\s]+) [NC]
RewriteRule ^ /platform/project-bids/%1/%2/? [NC,R,L]
# rewrites or internally maps the new url to the old one
RewriteRule ^platform/project-bids/(.*)/(.*)/?$ /platform/bidProject.php?pID=$1&name=$2 [L,NC]
Or
RewriteEngine on
RewriteCond %{ENV_REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^pid=([^&]+)&name=([^&]+)$ [NC]
RewriteRule ^platform/bidProject.php$ /platform/project-bids/%1/%2/? [NC,R,L]
RewriteRule ^platform/project-bids/(.*)/(.*)/?$ /platform/bidProject.php?pID=$1&name=$2 [L,NC]
Change R to R=301 (permanent redirect) when you are sure the rule is working ok.

mod_rewrite not excluding folder

I've setup some rewrite rules to cleanup the URLs of my page. These rewrites work fine, except for some downloads where i don't want to rewrite the URL.
I've tried several Rules to exclude the "files" folder:
1#:
RewriteCond %{REQUEST_URI} !/files[0-9]*/
#Rewrite ulrs:
RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [L]
2#:
RewriteCond %{REQUEST_URI} !^./files
#Rewrite ulrs:
RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [L]
3#:
RewriteRule ^files - [L]
4#:
Putting a new .htaccess file in the "files" folder with
RewriteEngine Off
Are there any other methods I could try?
I do want to redirect to the pdf download i've setup without rewriting. just like:
<a href="files/myDownload.pdf">
If I quote the rewrite rule:
#RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [L]
It no longer rewrites the url and i can access my download, so i just need to get the folder excepted from the rule.
Do you have any Ideas or tips?
All the tries except for #2 should have worked. Please note that the order in which you put your RewriteRules is also very important.
Can you give the following a try:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/files/ [NC]
RewriteRule ^ index.php?%{QUERY_STRING} [L]

.htaccess rewrite to simultaneously change domain and remove path

My URL structure is currently as follows:
http://domain.com/folder/filename (CURRENT)
I want to change this so that I can use the following URL instead:
http://sub.domain.com/filename (NEW)
So accessing the CURRENT or the NEW url, should load the file located at the CURRENT url, but show the NEW url in the address bar. It should only apply to the "/folder/" path.
sub.domain.com is a mirror of domain.com, ie. they share the same file system and root directory.
This is what I have so far:
Options +FollowSymLinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/folder/?(.*)$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]
This is working, but is missing the rule to remove the "/folder/" from the path. I've tried combining multiple RewriteRule's with no luck. Any ideas? Thanks.
UPDATE: Thanks again #Gerben - I understand what your rules are doing now, but the second one isn't working for me. I suspect because it's conflicting with some other rewrite rules, in particular those of WordPress, which are lower down in my .htaccess file:
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Because of this the page ends up in a redirect loop, ie (from Chrome):
"The webpage at http://sub.domain.com/folder/index.php has resulted in too many redirects." - while the url I was originally trying to access was, for example, http://sub.domain.com/page
Any ideas?
Try:
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteRule ^(folder/)?(.*)$ http://sub.domain.com/$2 [R=301,L]
This will redirect everything to sub.domain.com, and remove the /folder part of the URI if it is there. If not, it redirects and leaves the URI untouched.
RewriteCond %{THE_REQUEST} /folder/
RewriteRule ^folder/(.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ folder/$1 [L]
# WordPress rules here
edit the second R=301 should not have been there
But this won't work, as wordpress has no way of knowing you want folder. You could add the Proxy flag to the rewrite, but then you need to change the rule above to not redirect on this internal proxy request.

.htaccess url rewrite - remove .php remove www. convert ?id=10 to /10

As the title says, I need a quite complex url rewrite mechanism for a web-app as .htaccess rule.
I've searched quite a lot now and tried hundred of different rewrite rules.
So, basically this is what I need:
User goes to: http://www.site.com/product.php?id=12
Server should redirect to: http://site.com/product/12
Once thing to mention:
not all pages do append id's.
So I also have: http://www.site.com/some/page.php
which then should redirect to: http://site.com/some/page
or from http://site.com/anotherone.php to http://site.com/anotherone
You help is much appreciated and thank you a lot in advance for you help!
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)\.php$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)\.php\?id=([0-9]+)$ http://%1/$1/$2 [R=301,L]
I supose that you, previously have this mod_rewrite rule active:
Users goes to http://site.com/product/12 and in the browser is showed this URL, and internaly, and only internaly, server serve http://www.site.com/product.php?id=12
Put the first RewriteCond and Rule this:
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(\w+)\.php\?id=(\d*)$ /$1/$2 [R=301]
And add another to remove the .php when ends with .php
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(\w+)\.php$ /$1 [R=301]

mod redirect and rewrites ( Part 2 )

Further to my previous questions i am getting my self into a mess, so i will try and lay my problem/s out as best as i can here.
I am tidying up my URL structure but have the problem that my urls are well indexed in Search engines, so i need to rewrite my urls while also redirect them.
With help earlier on a previous question i currently have the following in my .htaccess
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]
# Rewrite all index.php to root: / ( with perm redirect )
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mydomain.com/$1 [R=301]
# Rewrite A newly added section rewrite ( no redirect required )
RewriteRule ^products/digital_imaging/([^/]*)/([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]
# Rewrite dental news article to neat nice url
RewriteRule ^dental_news/([^/]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect [L]
#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]
So, this is all working as expected, but i have another url that ends in a index.php that sometimes but not always has php variables, this i also need to rewrite and redirect, by trying to do it myself i am creating a conflict of some sorts, and its not working as expected.
The URL i need to rewrite & redirect with a 301 is below
http://www.mydomain.com/news/dentistry_dental/index.php?month=February&year=2011
With the previous index.php rule it currently displays as
http://www.mydomain.com/news/dentistry_dental/?month=February&year=2011
Half way there i guess... I need it to be the following
http://www.mydomain.com/dental_news/February/2011
Baring in mind that the url sometimes has no variables as below
http://www.mydomain.com/news/dentistry_dental/index.php
Which is currently with the index.php rule above is showing as
http://www.mydomain.com/news/dentistry_dental/
This needs to be
http://www.mydomain.com/dental_news/
So yes, i am totally crap at this stuff, it is entirely new to me, so if i can get this sorted ill be a very happy bunny indeed!
Thanks All
EDIT
Ok my current .htaccess looks as below.
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]
# Rewrite all index.php to root: / ( with perm redirect )
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mydomain.com/$1 [R=301]
RewriteRule ^products/digital_imaging/([^/]*)/([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]
# Rewrite dental news article to neat nice url
RewriteRule ^dental_news/([^/]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect [L]
#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)
RewriteRule (.*) /dental_news/%1/%2? [R=301]
RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]
RewriteRule ^dental_news/$ /news/dentistry_dental/index.php [L]
As it stands with this, The following occurs
www.mydomain.com/news/dentistry_dental/index.php
-> www.mydomain.com/dental_news/
( ^ Correct )
www.mydomain.com/news/dentistry_dental/index.php?month=May&year=2011
-> www.mydomain.com/dental_news/April/2011
( ^ Wrong : Showing right path in url bar, but displaying article_detail.php instead of index.php with variables)
I am guessing that 2 of the rules are similar and one is picking up the index.php with year and month variables and sending to article_detail page. I might be wrong however and i have no idea how to fix.
EDIT #2
Current .htaccess as below.
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^dentalsupportuk.com [nc]
rewriterule ^(.*)$ http://www.dentalsupportuk.com/$1 [r=301,nc]
# Rewrite all index.php to root: / ( with perm redirect )
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.dentalsupportuk.com/$1 [R=301]
RewriteRule ^products/digital_imaging/([^/]*)/([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]
# Rewrite dental news article to neat nice url
RewriteRule ^dental_news/([0-9]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect [L]
#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)
RewriteRule (.*) /dental_news/%1/%2? [R=301]
RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]
RewriteRule ^dental_news/([a-zA-Z]*)/([0-9]*)$ news/dentistry_dental/index.php?month=$1&year=$2&redirect [L]
www.mydomain.com/dental_news/ is now rewriting to www.mydomain.com/dental_news/ which doesnt exist so 404 error ( should be rewriting to www.mydomain.com/news/dentistry_dental/ )
www.mydomain.com/dental_news/100/some-title is now rewriting as expected to www.mydomain.com/news/dentistry_dental/article_detail.php with the variables ( working )
www.mydomain.com/news/dentistry_dental/article_detail.php?article=100&title=some-title is redirecting as expected to www.mydomain.com/dental_news/100/some-title and displaying correctly ( working )
www.mydomain.com/news/dentistry_dental/index.php?month=May&year=2010 is redirecting but crashing out Firefox stating ( Firefox has detected that the server is redirecting the request for this address in a way that will never complete. ) ( Not Working, is this some sort of loop causing this? )
www.mydomain.com/dental_news/July/2010/ the rewriting is giving me a 404 error.
So yes, im all over the place, maybe ive put in wrong order or something, but the more i mess the more im scared of breaking everything. Any ideas?
Regards
M
Try adding the follow two rules:
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)
RewriteRule (.*) /dental_news/%1/%2? [R=301]
RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]
I don't believe it causes any conflicts, and seems to cover the two scenarios you described.
EDIT
OK, so if I'm getting this correct, the URLs that are /dental_news/2968/Redefining-oral-hygiene-intervention should be rewritten to /news/dentistry_dental/article_detail.php with the proper querystrings. And then the /dental_news/April/2011 should be rewritten to /news/dentistry_dental/index.php. So...if that is the case, you should be able to add the following rule:
RewriteRule ^dental_news/([a-zA-Z]*)/([0-9]*)$ news/dentistry_dental/index.php?month=$1&year=$2&redirect [L]
It would also probably make sense to make the following update:
Current: RewriteRule ^dental_news/([^/]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect [L]
Updated: RewriteRule ^dental_news/([0-9]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect [L]
The difference there being that it is more specific, and the first regex group will only match a group of digits, and will help to avoid conflict with your rule that is rewriting to the index.php file.
EDIT 2
I believe I tested all of the URLs you provided, and they seem to be working now. There were a couple of redirect loops that we weren't accounting for. Hopefully this will help...
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]
# Rewrite all index.php to root: / ( with perm redirect )
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mydomain.com/$1 [R=301]
RewriteRule ^products/digital_imaging/([^/]*)/([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]
RewriteRule dental_news/$ /news/dentistry_dental/?rewrite [L]
# Rewrite dental news article to neat nice url
# Protect from looping because of previous rules
RewriteCond %{QUERY_STRING} !rewrite
RewriteRule ^dental_news/([0-9]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&rewrite [L]
#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [R=301]
# Protect from looping because of previous rules
RewriteCond %{QUERY_STRING} !rewrite
RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]
# Protect from looping because of previous rules
RewriteCond %{QUERY_STRING} !rewrite
RewriteRule ^dental_news/([a-zA-Z]*)/([0-9]*)/?$ news/dentistry_dental/index.php?month=$1&year=$2&rewrite [L]
Hope this helps.