htaccess rewrite rule does not work - apache

I am facing problem with rewrite rule in htaccess file. Please help.
This is my current htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /product-project/
RewriteRule . index.php [L]
</IfModule>
Now I want to create a rewrite rule such as "product/category/id" should redirect to "product/profile?profile_id=id". For this I have used
RewriteRule ^product/.*/(.*)$ product/profile?profile_id=$1 [L,R]
which is working fine in some way.
1st problem -> It is redirecting properly, but URL also changes. It should stay as "product/category/10", if I use the id as 10. But when I use this, it is redirecting properly, but URL changes to "product/profile?profile_id=10". I read somewhere to use [L,P] instead of [L,R] , but it is giving as Server error.
2nd problem -> Now I want to create a new rewrite rule such as "product/id" should also redirect to "product/profile?profile_id=id". For this I have used
RewriteRule ^product/(.*)$ product/profile?profile_id=$1 [L,R]
Now this is not working & showing server error
3rd Problem -> Can I also create a new rewrite rule such as "/id" should also redirect to "product/profile?profile_id=id". Is this possible ?
4th Problem -> Can I also create a new rewrite rule such as "/id1/id2" should also redirect to "product/profile?profile_id=id1&serial_id=id2". Is this possible ?
Thanks in advance for your time.

Is this what you want please check.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ $1/$2?profile_id=$3
for two id's
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ $1/$2?profile_id=$3&id=$4

Use it..
Options All -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Related

.htaccess URL rewrite not working, tried all possible options

I was trying to rewrite a URL for making my site SEO friendly, but .htaccess rewrite not seems to work.
My URL is
www.tasteofkochi.com/dine-detail.php?a=150
I need this to look like
www.tasteofkochi.com/sometext/150
I did the simple formula but it's not reflecting, intact nothing happens at all. If I add some junk char in htaccess, site crashes, which means htaccess is working fine. Also I added a formula to remove .php extn, and that too works fine. Only issue is with this one. Can anyone please help me. I enable rewrite in httpd and allow all in directories, still not working.
Below is my .htacces
RewriteEngine on
Options +FollowSymLinks -MultiViews
RewriteBase /
## hide .php extension
# To externally redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteRule ^detail/([0-9]+)/?$ dine-detail.php?a=$1 [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php?p=$1 [L,QSA]
We can create pretty urls with .htaccess and php by mainly two files one is .htaccess and another index.php
Example

.htaccess get lost query string

I have limited .htaccess knowledge and i am requiring some help. I need to do some redirects to enable pretty urls.
In local all works fine but it is not working in another develpment server. apparently the query string get drop when redirect.
i need to redirect this http://mysite.local/info/permalink/1
to this one http://mysite.local/info?proxy=true&id=1
my .htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#remove /index.php from home url and redirect to root.
#http://mysite.local/index.php -> http://mysite.local/
RewriteCond %{THE_REQUEST} ^.*\/index\.php?\ HTTP/
RewriteRule ^(.*)index\.php?$ "/$1" [R=301,L,QSA]
#pretty url without index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [PT,QSA,L]
#rewrite to handle some permalink saved on my db.
#http://mysite.local/info/permalink/1
#http://mysite.local/info/proxy=true&id=1
RewriteRule ^([^/]+)/info/([a-zA-Z0-9\-]+)/([0-9]+) /info/?proxy=true&id=$3 [L]
</IfModule>
the redirect is working but the query string is not present. When I run var_dump($_GET) on info module i am getting an empty array array(0) {}
i have try it to solve changing
RewriteRule .* index.php [PT,QSA,L]
to RewriteRule ^(.*)$ /%{QUERY_STRING} [L]
and
RewriteRule ^([^/]+)/info/([a-zA-Z0-9\-]+)/([0-9]+) /info/?proxy=true&idobj=$3 [L]
to
RewriteRule ^([^/]+)/info/([a-zA-Z0-9\-]+)/([0-9]+) /info/?proxy=true&idobj=$3 [QSA,NE,L]
Server API is CGI/FastCGI
What should I change to ensure that my rewrite works as intended and $_GET variables still are accessible?
thx in advance
I have no idea how you've managed to get this to work with a regex pattern like: ^([^/]+)/info if the URL you are going to is /info/permalink/1.
The ^([^/]+)/info pattern means there's stuff before the /info part of the URI, which there isn't. Additionally, in an htaccess file, the URI's have the leading forward slash stripped off. So you probably want something like this:
RewriteRule ^info/([a-zA-Z0-9\-]+)/([0-9]+) /info/?proxy=true&id=$2 [L]

Url renaming using .htaccess file

Most are for redirections and removing file extensions or for dynamic urls.
The problem being I can't get a simple static url to rename
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^fileThathasalongname file.html
What I currently have 'mysite.co.uk/fileThathasalongname.html'
What I want is 'mysite.co.uk/file/' while file = file.html
using:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^FileIWantToChange.html FriendlyNamed.html
Using this gives me the error multiple Choices
+++++++++++++++++Edit+++++++++++++++++++++++++
Thought i'd add my final version for people to look at, it may help, the anwser below is correct.
Options +FollowSymLinks -MultiViews
DirectorySlash Off
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteRule ^FriendlyNamed.html FileIWantToChange.html [L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
RewriteCond %{HTTP_HOST} ^www.mysire.co.uk [NC]
RewriteRule ^(.*)$ http://mysite.co.uk/$1 [L,R=301]
all works a charm!
I see multiple issues going on. Firstly the regular expression is matched against the friendly URL the user types in. So you need to swap your friendly url and file path with each other. The friendly or "fake" name goes on the left while the url to redirect to silently goes on the right. Also make sure you've set the directory base to /. Finally it's good to add an [L] to enforce it to be the last rule in case anything in the same file tries to rewrite the path. Due note that other htaccess files lower down, depending on the files location, will also be checked even when enforcing the rule has the last rule. Also junk the options part completely. Give this a try:
RewriteBase /
RewriteEngine On
RewriteRule ^FriendlyNamed.html FileIWantToChange.html [L]
RewriteRule ^fileThathasalongname.html file.html

subdomain htaccess

I have here a subdomain which i wish to pass on.
here the example of url : http://subdomain.domain.com/login
and it should point to : http://subdomain.domain.com/index.php/login
i write a simple htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|images|robots\.txt|css|javascript)
RewriteRule ^(.*)$ index.php/$1 [L]
but i always get 500 server error. any body have idea where i wrong?
thanks for any help
It's normal, you go to http://subdomain.domain.com/login, get redirected to http://subdomain.domain.com/index.php/login, then to http://subdomain.domain.com/index.php/index.php/login and so on because you RewriteRule always match.
You can write `RewriteRule ^([^/]*)$ index.php/$1 [L]
Make sure Apache rewrite module is active and your .htaccess file has the following line before any rewrite rule:
RewriteEngine On
Assuming you have mod_rewrite enabled, your RewriteRule causes an infinite redirection loop, which exceeds the maximum number of redirects and causes an internal server error.
You need to condition your rule so it only rewrites once. For example, this should work:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [L]

Need help with Apache Rewrite issues

My developer has provided me some Apache rewrite rules that are required for our application to work. When I added them to Apache my www.domain.com/blog and www.domain.com/phpmyadmin pages no longer worked. I tried to add the first RewriteCond rule for my blog and also the final phpmyadmin rule but neither one is working as expected. Essentially I want any requests to /blog or /phpmyadmin to NOT rewrite and go to my document root directory and run those applications outside of rewrites. Can you help me figure out a solution? Thanks
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^/(.*_css.*\.css.*) /$1 [QSA,L]
RewriteRule ^/(.*_js.*\.js.*) /$1 [QSA,L]
RewriteRule ^/(.*_swf.*\.swf.*) /$1 [QSA,L]
RewriteRule ^/(.*_img.*\.[jpg|JPG|jpeg|JPEG|gif|GIF|bmp|BMP|png|PNG].*) /$1 [QSA,L]
RewriteRule ^/(.*)$ /index.php?url=$1 [QSA,L]
RewriteRule ^/phpmyadmin(.*)$ /phpmyadmin$1 [QSA,L]
</VirtualHost>
They are located at www.domain.com/blog and www.domain.com/phpmyadmin.
Apache 2.2.13
Thanks!
If you want to avoid rewriting URLs that begin with /blog/ or /phpmyadmin/ then you may be able to get away with the first rule being:
RewriteRule ^/(?:blog|phpmyadmin)/ - [L]
(replacing your RewriteCond and then removing the old phpmyadmin rule).
A quick explanation: this matches any URLs that begin with /blog/ or /phpmyadmin/ and doesn't rewrite them (- for the replacement), and then stops any further rewriting ([L]).
Previous answer:
Your rewrite rules are conflicting, for a start. Also, at the moment, the RewriteCond only applies to the first rule. Also, the final rule is ignored because of the rule before it matching everything. You may want something like this:
RewriteRule ^/blog/(.*)$ /index.php?url=$1 [QSA,L]
as I assume you're just trying to rewrite all URLs from /blog/foo/bar to /index.php?url=foo%2fbar? If not, please explain what you're trying to accomplish and I'll edit my answer.
Err... not sure what you are trying to do. Are you trying to "Exclude" static files as images/stylesheets/flash movies/javascript files and the link to PHPMyAdmin? Maybe something like this could do the job for you 'quick 'n dirrrrty'
# Enable the RewriteEngine
RewriteEngine On
RewriteBase /
# Check if the file, directory or symlink does not already exists.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-F
RewriteCond %{REQUEST_FILENAME} !favicon\.ico
RewriteCond %{REQUEST_FILENAME} !robots\.txt
# Rewrite all other requests to the index.php
RewriteRule ^(.+)$ /index.php?url=$1 [QSA,L]