htaccess rewrite url and redirect - php

I am wondering how to rewrite this url profile.php?user=1 to /user/1.
I know that this can be done by .htaccess. Here is what i have tried and it worked fine but the only problem is that it
RewriteCond %{THE_REQUEST} \s/+profile\.php\?user=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ profile.php?user=$1 [L,QSA]
Besides these codes, my .htacces also has these lines
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.*$ [NC]
RewriteRule \.(gif|jpg|png)$ http://www.domain.com [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
ErrorDocument 404 /error404.php
ErrorDocument 403 /error404.php
Options -Indexes
RewriteCond %{THE_REQUEST} \s/+page\.php\?pagename=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /page.php?pagename=$1 [L,QSA]
Please also tell me if my .htaccess page is correct. I am not good at htaccess

Change this:
RewriteCond %{THE_REQUEST} \s/+profile\.php\?user=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ profile.php?user=$1 [L,QSA]
to:
RewriteCond %{THE_REQUEST} \s/+profile\.php\?user=([^\s&]+) [NC]
RewriteRule ^ /user/%1? [R=302,L]
RewriteRule ^user/([^/]+)/?$ profile.php?user=$1 [L,QSA]
And make sure the page has either absolute links or add this to the page's header:
<base href="/" />
otherwise, all your links will be broken.

In htacess rewrite you want to in fact redirect a url structure to a different url
If you have a link to profile.php?user=1 you will have to change this like to this : profile.php/user/1 then tell your htacess to redirect this adresse to this profile.php?user=1
You can do this :
RewriteRule ^user/(.*)$ index.php?&user=$1
If you want to test out some htaccess I will recomande this usedul site : http://htaccess.madewithlove.be/

Related

replacing ? by / and triming some text from url by htaccess

Hello all i am having a website where i have htaccess file the code inside the file seems like
ErrorDocument 404 /error404.php
ErrorDocument 403 /error404.php
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule ^ - [L]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.*$ [NC]
RewriteRule \.(gif|jpg|jpeg)$ http://www.domain.com [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/+(page1)\.php\?eid=(78)[&\s] [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteRule ^(page1)/(\d+)$ /$1.php?eid=$2 [L,QSA,NC]
RewriteCond %{THE_REQUEST} \s/+page2\.php\?url=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /page2.php?url=$1 [L,QSA]
The file successfully redirects the page2.php?url=xyz to page2/xyz but it directs the page1.php?eid=3 to page1?eid=3 besides this i also needs to direct the page3.php?did=3 to page3/3
I need the page2.php?eid=3 to be directed to page2/3
and page3.php?did=4 to page3/4
page1 is directed fine.
Can any one help me actually i am not so good in htaccess but i have tried to manipulate the htacces file but no help.
Thanks

htaccess redirection working for on file but not working for other files

Below is my htaccess file.
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteBase /
ErrorDocument 404 /404.php
RewriteRule ^([^/]+)/(\d+)/$ $1/$2 [R=301,L]
RewriteCond %{THE_REQUEST} \s/view_campaign\.php\?var1=([0-9]+)\s [NC]
RewriteRule ^ view_campaign/%1? [R=301,L]
RewriteRule ^view_campaign/([0-9]+)$ /view_campaign.php?var1=$1 [L]
RewriteRule ^([^/]+)/(\d+)/([^/]+)$ view_campaign.php?var1=$2&var2=$3 [NC,L]
RewriteCond %{THE_REQUEST} \s/profile_view\.php\?id1=([0-9]+)\s [NC]
RewriteRule ^ profile_view/%1? [R=301,L]
RewriteRule ^profile_view/([0-9]+)$ /profile_view.php?id1=$1 [L]
RewriteRule ^([^/]+)/(\d+)/([^/]+)$ profile_view.php?id1=$2&name2=$3 [NC,L]
and here are my urls/
http://localhost/profile_view/1/test
http://localhost/view_campaing/1/test-campaign-name
with some php code my link for view_campaign is redirecting properly. i can say my links for view_campaign is working fine. if it appears like below.
http://localhost/view_campaign/1/test-campaign-name-some-junk
http://localhost/view_campaign/1
http://localhost/view_campaign/1/
I want the same for profile_view. i copy pasted the same code in htaccess/php file and change relevant things.
Issue is if i try to open http://localhost/profile_view/1/test or http://localhost/profile_view/1 or http://localhost/profile_view/1/
it is redirecting to
http://localhost/view_campaign/1/test-campaign-name-some-junk
I don't have much understanding about htaccess.
Please advise.
Have it this way:
ErrorDocument 404 /404.php
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/(\d+)/$ $1/$2 [R=301,L]
RewriteCond %{THE_REQUEST} \s/view_campaign\.php\?var1=([0-9]+)\s [NC]
RewriteRule ^ view_campaign/%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/profile_view\.php\?id1=([0-9]+)\s [NC]
RewriteRule ^ profile_view/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^(view_campaign|profile_view)/(\d+)$ $1.php?var1=$2 [L,NC,QSA]
RewriteRule ^(view_campaign|profile_view)/(\d+)/([^/]+)$ $1.php?var1=$2&var2=$3 [NC,L,QSA]
I make some changes in anubhava answer. Now, it is working fine.
Below is the working code.
ErrorDocument 404 /404.php
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/(\d+)/$ $1/$2 [R=301,L]
RewriteCond %{THE_REQUEST} \s/view_campaign\.php\?var1=([0-9]+)\s [NC]
RewriteRule ^ view_campaign/%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/profile_view\.php\?var1=([0-9]+)\s [NC]
RewriteRule ^ profile_view/%1? [R=301,L]
RewriteRule ^view_campaign/([0-9]+)$ /view_campaign.php?var1=$1 [L]
RewriteRule ^(view_campaign)/(\d+)/([^/]+)$ view_campaign.php?var1=$2&var2=$3 [NC,L]
RewriteRule ^profile_view/([0-9]+)$ /profile_view.php?var1=$1 [L]
RewriteRule ^(profile_view)/(\d+)/([^/]+)$ profile_view.php?var1=$2&var2=$3 [NC,L]

htaccess redirect domain with query string to subdomain with excludes

We have moved our site to a subdomain while on the main domain we have a new site with new url's. What we are trying to do is having the old url's redirect to the subdomain while the excluding the new url's.
here's examples of the old url's:
http://www.domain.co.il/index.php?dir=app_admin&page=ip_stat&op=list&pos=0
should be redirected to:
http://sub.domain.co.il/index.php?dir=app_admin&page=ip_stat&op=list&pos=0
While the new site url's (do not contain php) and the homepage should not be effected.
This is what our htaccess looks like:
It does redirect the url's to the sub, but does not exclude the new pages and redirect them to the subdomain home (sub.domain.com)
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !contact(|$)
RewriteCond %{REQUEST_URI} !about(|$)
RewriteCond %{REQUEST_URI} index\.php
RewriteRule ^(.*)$ http://sub.domain.com [R=301,L]
Hopefully someone can help us get this right, we get many 404 in our gwt now :/
Thanks for any help !
Fix some regex and reorder your rules:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} !/(contact|about) [NC]
RewriteCond %{THE_REQUEST} !\.(jpe?g|gif|bmp|png|tiff|css|js) [NC]
RewriteRule ^(.+)$ http://sub.domain.com/$1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Also test this in a new browser to avoid old browser cache.
Here's the full code for the site.
The way things are set up is that the new site is being called from a folder named "main" while the old site is being called from the root directory.
So we actually have 2 htaccess.
Here's the root htaccess code:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^/([^.]+)\.php
RewriteRule ^(.*)$ http://sub.domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} www.domain.com$
RewriteCond %{REQUEST_URI} !^/main
RewriteRule ^(.*)$ /main/$1 [L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteRule ^admin$ index.php?dir=app_misc&page=login [L]
RewriteRule ^admin/$ index.php?dir=app_misc&page=login [L]
RewriteRule ^sitemap.xml$ sitemap.php [L]
RewriteRule ^robots.txt$ robots.php [L]
RewriteRule ^adv$ adv/index.php [L]
RewriteRule ^adv/$ adv/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(([0-9]+)_([^/]+)|([0-9]+))$
RewriteRule (.*) /404.php?a=%{REQUEST_URI} [L]
and the htaccess in the "main" folder:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !(contact|about) [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
I've tested on few browsers and in incognito mode as well
Thanks
Instead of excluding urls which caused necessary css & js files to redirect as well, I solved it by creating a redirect that effect only url's with php.
RewriteCond %{THE_REQUEST} ([^.]+)\.php [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L,NE]

rewriting www to non-www and index.php CI

I am using CodeIgnter, as for a result all my links are like base.com/index.php/home/index or www.base.com/index.php/home/index.
I would like to display them only as base.com/home/index if possible.I have tried looking over the internet,got the rewrite in htacces from both of them as:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$0 [PT,L]
RewriteCond %{HTTP_HOST} ^domain\.com\$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com\$
RewriteRule ^/?$ "http\:\/\/domain\.com\/" [R=301,L]
and
RewriteEngine on
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
put them separately,they work.But toghether they don't do what i need them to do.
Anyone knowing the solution?Thanks.
For CI, you want something like your first set of rules, but without the redirect. But the redirect needs to happen before the routing happes, so try:
RewriteEngine On
# redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.base\.com$ [NC]
RewriteRule ^(.*)$ http://base.com/$1 [L,R=301]
# redirect direct requests to /index.php to remove it
RewriteCond %{THE_REQUEST} \ /index\.php/?([^\?\ ]*)
RewriteRule ^ http://base.com/%1 [L,R=301]
# internally route to /index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [PT,L]

are these rewrite rules correct to archive following things?

i wanted to
redirect all users to domain with WWW
im using codeigniter so wanted to remove index.php from url's
prevent access to index.php and redirect to domain name if someone request domain/index.php
wanted to 301 redirect all pages
so these are the lines i came up with .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)$
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ http://%{HTTP_HOST}/$1 [R=301,L]
Are these rules correct to do what i wanted to do? is there better ways to do this?
Regards
I have made some changes and removed some redundant code. Here is your modified .htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
Also make sure your css, js, images files always use absolute path instead of relative one i.e. path to js, css, images should either start with http:// or /.

Categories