Using .htaccess to route friendly url - .htaccess

I am trying to route an url which is displayed as follows www.example.com/profile/1 to the following page with parameters www.example.com/profile?user=1. I have the following RewriteRule in my .htaccess, yet the routing is not working. Can someone tell me what I am doing wrong?
RewriteEngineOn
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/profile/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) /profile?user=$1 [L,QSA]
I have tried switching up the last RewriteRule to different URL's, but so far no luck. I am probably overseeing a small problem which someone can hopefully help me out with.

You need to allow for profile/\w+ as regex pattern to match /profile/123:
Options -MultiViews
RewriteEngineOn
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^profile/(\w+)/?$ /profile?user=$1 [L,QSA,NC]

Related

.htaccess not every rewriterule works

I have the following .htaccess file
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
RewriteRule ^help$ /pages/help/index.php [NC]
RewriteRule ^help/search$ /pages/help/search.php [NC]
RewriteRule ^help/support$ /pages/help/support.php [NC]
RewriteRule ^help/bugfixes$ /pages/help/bugfixes.php [NC]
RewriteRule ^help/([a-z0-9-]+)$ /pages/help/category.php?cat_slug=$1 [NC]
RewriteRule ^help/([a-z0-9-]+)/([0-9]+)/([a-z0-9-]+)$ /pages/help/article.php?cat_slug=$1&art_id=$2&art_slug=$3 [NC]
The problem is the following:
When I go to localhost/help, it redirects to localhost/C:/server/htdocs, so I don't see the index page. The same happens when i go to localhost/help/bugfixes.
The other pages work perfectly! And untill today, /help and /help/bugfixes did also. :/
If I change ^help/bugfixes$ to ^help/bugfix$ (for example), the bugfixes-pages is showed again. If I change ^help$ to ^help/help$ (or any other thing), it also works for the index page.
I have literally no idea what could be wrong. I restarted apache already a couple of times, but still nothing... I didn't changed anything in the file for the last 2 days.
Can someone help me?
Edit: I also didn't changed anything in the php-files index.php and bugfixes.php
Try this htaccess :
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteRule ^ /%1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^help/(.*)$ /pages/help/$1.php [NC,L]
RewriteRule ^help/([^/]+)/?$ /pages/help/category.php?cat_slug=$1 [L]
RewriteRule ^help/([^/]+)/([^/]+)/([^/]+)/?$ /pages/help/artical.php?cat_slug=$1&art_id=$2&art_slug=$3 [L]

Weird behavior with multiple conditions

I have the following rules:
RewriteCond %{REQUEST_URI} !script.php
RewriteCond %{QUERY_STRING} url=(.*)
RewriteRule .* script.php?q=%1
RewriteCond %{REQUEST_URI} !script.php
RewriteRule (.*) script.php?q=$1
What I'm trying to accomplish here is that every access to http://example.com/?url=www.google.com is redirected to script.php?q=www.google.com and if there is no query string, relative path should be sent to script.php, for example in http://example.com/path, should be redirected to script.php?q=path. This doesn't seem to work with my rules, but I'm not sure why. Is this even possible ?
You can have your rules as this:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^url=([^&]+) [NC]
RewriteRule ^/?$ script.php?q=%1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.+)$ script.php?q=$1 [L,QSA]

website proper redirecting via .htaccess

I searched about .htaccess redirecting address after first symbol / but I can't get it working.
.htaccess file is like this one:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /index.php?route=/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^OldDomain [NC]
RewriteRule ^blog/(.*)$ NewDomain/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.OldDomain [NC]
RewriteRule ^blog/(.*)$ http://www.NewDomain/$1 [R=301,L]
When I test it with the domain
www.OldDomain.com/catalog
its redirects to
www.NewDomain.eucatalog
which does not have the slash symbol "/".
How can I make the redirect keep the slash symbol?
This may be unrelated, but you need your redirect rules (the last 2) to come before your routing rule. It's possible your router (index.php) is redirecting incorrectly:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^OldDomain [NC]
RewriteRule ^blog/(.*)$ NewDomain/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.OldDomain [NC]
RewriteRule ^blog/(.*)$ http://www.NewDomain/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /index.php?route=/$1 [L,QSA]

.htaccess single variable url rewrite

I am trying to redirect http://test.pearlsquirrel.com/explore_all?u=hiphop to http://test.pearlsquirrel.com/explore_all/hiphop using htaccess. However, It keeps redirecting here: http://test.pearlsquirrel.com/explore_all/?u=site_error.php. I have looked all over Stackoverflow for the answer but I just can't seem to find it. I would really appreciate some help in fixing this problem, thanks!
Here is my .htaccess file
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.pearlsquirrel.com$ [NC]
RewriteRule ^(.*)$ http://pearlsquirrel.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+) explore_all?u=$1 [L]
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} [^=]+=([^=]+)
RewriteRule .* /%1? [L]
Will map this:
http://test.pearlsquirrel.com/explore_all?u=hiphop
To this:
http://test.pearlsquirrel.com/explore_all/hiphop
Replace the corresponding code in your .htaccess file with this one.
I have not checked the rules in your .htaccess file.
OPTION
Now, if it is the other way around, which I think it is, and the idea is to map this incoming URL:
http://test.pearlsquirrel.com/explore_all/hiphop
To this resource:
http://test.pearlsquirrel.com/explore_all?u=hiphop
Replace the above rule set with this one:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} .*/([^/]+)/?
RewriteRule .* http://test.pearlsquirrel.com/explore_all?u=%1 [L]

htaccess redirect from subdirectory to subdirectory

I'm trying to set up a local instance of a php site working in a server, in the "/" directory. My local instance will work in my development environment in a "/subdirectory/".
I'm trying to translate .htaccess to adapt to this, but I always get a 404. This is original .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^my-nice-url$ /index.php?p=ugly_url
And these are my tries:
RewriteEngine on
RewriteBase /mylocaldirectory/
# Not in local! RewriteCond %{HTTP_HOST} ^example.com$ [NC]
# Not in local! RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^my-nice-url$ /index.php?p=ugly_url
Also
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)my-nice-url$ /index.php?p=ugly_url
Thank you
If i got you right, you want make a user friendly url, i do it like this:
RewriteRule ^(.*)/(your-nice-url)$ ($1)/$2/ [R=301]
RewriteRule ^(.*)/(your-nice-url)/$ $1/index.php?p=$2 [L]
or
RewriteRule ^(.*)/(your-nice-url)$ ($1)/$2/ [R=301]
RewriteRule ^(.*)/(your-nice-url)/$ $1/index.php?p=what-ever-you-need [L]
Hope it help you.

Resources