Simple mod_rewrite rule not working [L] - php

So this is my first time doing mod_rewrite and I'm using tutorials and generators and it's been going okay so far except for the last line below which if I haven't made any mistake should redirect about.php to about/
Anybody know why it doesn't ?
All the other rules are working without issues (I did double check the page existed, I tried to add a slash before about.php as my website is on a server with multiple websites...)
Options +FollowSymLinks
#Sub-dir e.g: /cmsms
RewriteBase /
#redirect www.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.metareviewr.com
RewriteRule (.*) http://metareviewr.com/$1 [R=301,L]
#RewriteRule ^show/([^/]*)/$ /tvshow.php?id=$1 [L]
RewriteRule ^show/([^/]*)/([^/]*)/$ /tvshow.php?id=$1&?name=$2 [L]
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://metareviewr.com/$1 [R=301,L]
RewriteRule ^about/$ about.php [L]

This should work
RewriteRule ^about.php$ about/ [L,R=301]

Related

htaccess & 301 redirects causing server slowdowns

My .htaccess file looks like this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [L,R=301]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule ^resources/([^/\.]+)/?$ page.php?theme=resources&pg=$1 [L]
RewriteRule ^resources/([^/\.]+)/?/([^/\.]+)/?$ page.php?theme=resources&pg=resources&catname=$1&cat=$2 [L]
RewriteRule ^resources/([^/\.]+)/?/([^/\.]+)/?/([^/\.]+)/?$ page.php?theme=resources&catname=$1&cat=$2&pg=$3 [L]
RewriteRule ^albums/([^/\.]+)/?/([^/\.]+)/?$ page.php?theme=albums&pg=albums&albumname=$1&album=$2 [L]
RewriteRule ^albums/([^/\.]+)/?$ page.php?theme=albums&pg=albums [L]
RewriteRule ^leaving-the-site/([^/\.]+)/?$ page.php?theme=leaving-the-site&pg=leaving-the-site&q=$1 [L]
RewriteRule ^([^/\.]+)/?/([^/\.]+)/?$ page.php?theme=$1&pg=$2 [L]
RewriteRule ^([^/\.]+)/?$ page.php?theme=$1&pg=$1 [L]
Everything seems fine, but as we're relaunching the site, we have approximately 110 301 redirects we need to put in place. These are similarly formatted to the following, and added after the above rewrite rules:
RewriteRule ^about/about-the-site$ https://www.mywebsite.com/about-us? [L,NC,R=301]
My problem is that as soon as I have the htaccess file uploaded with all the 301s, it starts to slow down the site, to the point of having 60 second load times within about 10 minutes and then starts timing out.
I remove the 301 redirects, and it recovers. From my research, 301 redirects can slow down a server, but 'not significantly', at least not with less than 1000 lines.
Is there something I should be doing differently?
This is combined HTTPS/SSL & www to non-www Rewrite|Redirect htaccess code specifically for WordPress, but you can modify/customize it for your particular site. I am still looking at your other code...
Edit: Ok I looked at your other code and it is overly complex and the use of question marks in your code may be incorrect. What I recommend you do is use RewriteCond Query String matching code instead of the direction/code you are trying to use now. See this topic for example RewriteCond Query String matching htaccess code examples: Replace "index.php?option=com_k2&view=item&id=" using .htaccess and Regex
# WP REWRITE LOOP START
# Rewrite|Redirect http to https|SSL & www to non-www
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteCond %{SERVER_PORT} ^80
RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteRule ^index\.php$ - [L]
# All lines of Rewrite|Redirect code would go here
# if file or directory does not exist then Rewrite to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# WP REWRITE LOOP END

Need to write multiple htaccess rule with multiple parameters

I've some rules already written in htaccess to convert .php extension to HTML and they are working fine but now I need to write rule for different page with same parameter but it if I set same rule for different page its not working its redirecting on same page.
Below is my htaccess file code
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /apnaujjain/
# add everything for webservice/...
RewriteRule ^webservice(/.*)?$ - [L,NC]
#redirect localhost/apnaujjain/blog.php?page_id=1&post_id=1&action=blog to localhost/apnaujjain/1/1/blog.html
#RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&album_id=([^\s&]+)&action=([^\s&]+) [NC]
#RewriteRule . %1/%2/%3.html? [R=301,L]
#RewriteRule ^([^/]+)/([^/]+)/([^./]+)\.html$ blog.php?page_id=$1&album_id=$2&action=$3 [NC,L,QSA]
#redirect localhost/apnaujjain/page.php?page_id=1&album_id=1&action=contacts to localhost/apnaujjain/1/1/contacts.html
RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&album_id=([^\s&]+)&action=([^\s&]+) [NC]
RewriteRule . %1/%2/%3.html? [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/([^./]+)\.html$ page.php?page_id=$1&album_id=$2&action=$3 [NC,L,QSA]
#redirect localhost/apnaujjain/page.php?page_id=1&action=contacts to localhost/apnaujjain/1/contacts.html
RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&action=([^\s&]+)\s [NC]
RewriteRule . %1/%2.html? [R=301,L]
RewriteRule ^([^/]+)/([^./]+)\.html$ page.php?page_id=$1&action=$2 [NC,L,QSA]
#redirect localhost/apnaujjain/contacts.php to localhost/apnaujjain/contacts.html
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php\s [NC]
#RewriteRule ^(admin|webservice)($|/) - [L]
RewriteRule !^admin/ /%1.html [NC,R=302,L,NE]
RewriteRule ^(.+?)\.html$ $1.php [L,NC]
I have already written comment on every rule for what its doing. Now currently all rules are working but when I added other rule it stopped working so I just commented that rule.
Rule for page.php is working but when I change page to blog.php some rules work but some rule with multiple parameter with blog.php doesn't work.
Any help is really appreciated
You can use this rule to route localhost/apnaujjain/1/blog to http://localhost/apnaujjain/blog_detail.php?post_id=1&action=blog:
RewriteCond %{THE_REQUEST} /blog_detail\.php\?post_id(\d+)&action=([^\s&]+)\s [NC]
RewriteRule ^ %1/%2.html? [R=302,L,NE]
RewriteRule ^(\d+)/(blog)\.html$ blog_detail.php?post_id=$1&action=$2 [L,QSA,NC]

Remove/Redirect index.php from url to prevent duplicate urls

Please read the question carefully before marking as duplicate.
We all know, that using in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
we can redirect all traffic to index.php so we can create friendly urls and have one front controller.
Although the question is connected to mod_rewrite the problem is described for Laravel.
The following .htaccess comes by default with Laravel 4 and it works fine:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
If we run url mydomain.com/something and have set that route for something properly, some controller will be launched. It works fine so far.
However in Laravel 4 we will be able to reach the same route using mydomain.com/index.php/something. Probably using Laravel url creating we will have no urls with index.php in url but there is some other problem.
For example if our competition would like to make us some harm, they can simple put in Internet single links for urls to mydomain.com/index.php/something, mydomain.com/index.php/something2 and so on and search engines will see duplicate urls.
Of course if we have our custom PHP application, we can do it in PHP without a problem checking simply $_SERVER['REQUEST_URI'] and make 301 redirection. We can of course do the same in Laravel but we have to write this code in PHP each time and probably some developers could say it is bad practice to do it in PHP.
Question is simple: how can I redirect in .htaccess all urls that contain index.php to to the same url without index.php?
Example urls that should be redirected:
mydomain.com/index.php/something should be redirected to mydomain.com/something (something could be anything - can contain any characters)
mydomain.com/index.php should be redirected to mydomain.com
mydomain.com/index.php?anything should be redirected to mydomain.com (anything can contain any characters)
mydomain.com/index.phpanything should be redirected to mydomain.com anything can contain any characters)
Insert these rules just below RewriteEngine On line:
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php[^/] /$1? [L,R=302,NC,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [L,R=302,NC,NE]
After spending hours I write below code for me and its 100% working
Redirect index.php to non index.php
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^index.php/(.*)$ /$1 [R=301,L]
how can I redirect in .htaccess all urls that contain index.php to to
the same url without index.php?
Add this to your .htaccess
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
For Nginx, here is the rules :
location / {
rewrite ^/(.*?)index\.php[^/] /$1? redirect;
rewrite ^/(.*?)index\.php(?:/(.*))?$ /$1$2? redirect;
}
This solved my problem to force https & remove index.php from the url in Kohan 2.3
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php[^/] /$1? [L,R=302,NC,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [L,R=302,NC,NE]
RewriteRule ^(application|system) - [F,L]
RewriteCond %{THE_REQUEST} /index.php [NC]
RewriteRule ^(.*)index\.php$ /$1/ [R=301,L,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?!localhost$|127\.0\.0\.1$)(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

URL Rewrite for multiple pages but same query

As of now I have one generic query "view" which I'll use on most pages...
My rewrite is:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ $1.php?view=$2 [L]
My url is:
classroom.php?view=creation
I want it to look like:
classroom/creation
I have it working but it screwed up other urls
dashboard.php was just /dashboard
but broke when I did the first rewrite rule...
These things are a pain to understand.
My Question is how can i have /dashboard, /classroom, /classroom/creation work?
EDIT: I got it working with:
RewriteRule ^([a-zA-Z0-9_-]+)$ $1.php [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ $1.php?view=$2 [L]
The first 2 rules are for external redirect, which turns .php into a directory like format rest of the rules are for the internal redirect which keeps the pretty URL and doesn't show the redirect.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Redirect /classroom.php?view=creation to /classroom/creation
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+classroom\.php\?view=([^\s]+) [NC]
RewriteRule ^ /classroom/%1? [R=302,L]
# Redirect /classroom.php to /classroom and /dashboard.php to /dashboard
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(classroom|dashboard)\.php [NC]
RewriteRule ^ /%1 [R=302,L]
# Internally forward /classroom/creation to /classroom.php?view=creation
RewriteRule ^classroom/(.*)/?$ /classroom.php?view=$1 [L,QSA,NC]
# Internally forward /classroom to /classroom.php and /dashboard to /dashboard.php
RewriteRule ^(classroom|dashboard)/?$ /$1.php [L,QSA,NC]

Htaccess issues - Redirecting from pretty url to php version

I am not a new developer and have worked with .htaccess for years, but today I'm totally stuck and have no idea what is going on. Below is my full htaccess file:
Options All -Indexes
ReWriteEngine On
RewriteRule ^page/([a-zA-Z0-9_-]+)$ page.php?pageurl=$1
RewriteRule ^([a-zA-Z0-9_-]+)$ category.php?categoryurl=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([0-9]+)-([a-zA-Z0-9_-]+)$ product.php?category=$1&subcategory=$2&productid=$3&producturl=$4
RewriteCond %{HTTP_HOST} www.website\.com$ [NC]
RewriteRule ^(.*)$ http://website.com/$1 [R=301,L]
website.com/category should take me to the category page which it does, but for some reason the url gets changed, as if htaccess is working in reverse, I get sent to website.com/category.php?categoryurl=category.
This is exactly the same for the other ones, my product page should be website.com/category/subcategory/123-producturl but it redirects too:
website.com/product.php/category/123-producturl?category=category&subcategory=subcategory&productid=123&producturl=producturl
Anybody have any idea at all why this is doing what it is?
Try this one, you have not added flags to end the line in HTACCESS.
Options All -Indexes
ReWriteEngine On
RewriteRule ^page/([a-zA-Z0-9_-]+)$ page.php?pageurl=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)$ category.php?categoryurl=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([0-9]+)-([a-zA-Z0-9_-]+)$ product.php?category=$1&subcategory=$2&productid=$3&producturl=$4 [L]
RewriteCond %{HTTP_HOST} www.website\.com$ [NC]
RewriteRule ^(.*)$ http://website.com/$1 [R=301,L]

Categories