I am trying to implement URL rewriting in my PHP application. Can someone share a step by step procedure of implementing URL rewriting in .htaccess.
In my application I want to implement following URL
www.domain.com/shop/shop.php?shopname=myshop&sh=1
to
www.domain.com/shop/myshop
i use the follwing
RewriteEngine On
RewriteBase /shop/
RewriteCond %{THE_REQUEST} /shop\.php\?shopname=([^\s&]+)&sh=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ shop.php?shopname=$1&sh=$2 [L,QSA,NC]*
It shows the url like www.domain.com/shop/myshop
But not pointing to www.domain.com/shop/shop.php?shopname=myshop&sh=1
If I understand you correctly, you can do what you want with:
RewriteCond %{QUERY_STRING} ^id=([^&]*)&name=(.*)$
RewriteRule ^/shop/shop.php$ /shop/$2?id=$1
The RewriteCond check for a query string of the correct format and extracts the values. $1 = the ID and $2 = the name.
The RewriteRule rewrites a URL of the correct format to the new format, inserting the name and id parameters from the RewriteCond.
Related
I would like to show certain content on my website. I choose the content based on a URL parameter, like page=about. This works well, and my URL looks like example.com/mysite/index.php?page=about. My issue is that I would like my URL to read example.com/mysite/about, where the last part is the name of the page parameter.
I am attempting to do this through htaccess. This is my exact code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
I see no change on my page nor to the URL structure. The URL still reads ...index.php?page=about. Am I doing something wrong? I tried this a bunch of different ways and I still get the same result. I also tried simply doing this, with no change:
RewriteEngine On
RewriteRule ^([a-z]+)\/([0-9]+)\/?$ index.php?page=$1 [NC]
I didn't think it was working, so I tried a redirect to some other site and it worked. Therefore, the htaccess file is getting read and able to function.
Any assistance would be helpful!
Thank you,
BP
You may use these 2 rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+(mysite)/index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/mysite/([^/]+)/?$ [NC]
RewriteRule ^ index.php?page=%1 [L,QSA]
I want to rewrite some url by using .htaccess rewrite
I have url something like this:
domain.com/index?/pid=10550
and want to rewrite users to
domain.com/index.php?pid=10550
So, what will happen is users will access that url in backend
I'm new to php and have read some blogs like this have no luck
Also, I have tried this
Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^domain.com/index?/$ /domain.com/index.php? [L]
it says The page isn't redirecting properly
and some times shows me a directory
You can not match against querystring in RewriteRule's pattern. You will have to match against {THE_REQUEST} variable (a full request line sent by a client to the server) using a RewriteCond
RewriteEngine on
#Remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]
#redirect /index.php/?pid=123 to /index.php?pid=123
RewriteCond %{THE_REQUEST} /index\.php/\?pid=([^\s]+) [NC]
RewriteRule ^ /index?pid=%1 [L,R]
This will redirect /index.php/?pid=123 to /index.php?pid=123 .
I've an https domain with WordPress installed in the root directory.
In the subdirectory "/test/" i made a web application that works using GET; the path is something like that:
https://www.example.com/test/file.php?url=urlname&etc=etc
I need to transform it in:
https://www.example.com/test/urlname/?etc=etc
I also need a redirect from the first type url to the second one.
It's the first time that i have to edit an htaccess file, and after searching on the web i tried this code
RewriteRule ^/?test/([^/]+)/$ test/file.php?url=$1&%{QUERY_STRING} [L,QSA]
RewriteCond %{REQUEST_URI} ^/test/file\.php$
RewriteCond %{QUERY_STRING} ^url=(.*)$
RewriteRule ^/?test/file\.php$ /test/%1/?%{QUERY_STRING} [L,R=301]
but it obviously doesn't work.
Can someone help me?
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /test/file\.php\?url=([^\s&]+)(?:&(\S*))?\s [NC]
RewriteRule ^ /test/%1?%2 [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^test/([^/]+)/?$ test/file.php?url=$1 [L,QSA,NC]
I am trying to rewrite the following URL via .htaccess:
http://website.com/dealer_home.php?id=dealer1
The result I am aiming for is this, where dealer1 is the username which is set as variable:
http://website.com/dealer1
I have tried this rule in .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)$ /dealer_home.php?id=$1 [L]
However I get "Internal Server Error" message when trying to load any of the website pages.
Can you provide some advice where I am doing wrong?
EDIT:
I tried also RewriteRule ^(.*)$ dealer_home.php?id=$1 [PT] but no success.
Thank you!
Maybe it's a conflict with existing files/folders and root uri.
Try this code instead
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]
This rule will match every url like domain.com/something if something is not an existing file or folder.
So if you have other rules then you should put them above this one.
EDIT: to avoid duplicate content and to redirect old format to new url format
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/dealer_home\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]
I am knocking my head how to rewrite get parameters with htaccess.
Here is my htaccess so far (removing only index.php).
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
I want to rewrite the following url http://example.com/?lang=en&dscroll=1200 into http://example.com/en/dscroll/1200. Some htaccess master, please :)?
Thanks
The rule that you have doesn't remove the index.php, it only redirects to a host without the www in front.
As for the other rewrite, you need 2. First, you need to make sure your links in all of your content looks like this:
http://example.com/en/dscroll/1200
instead of the one that has the query string.
Then you need to add, below the rule that you already have, these 2 rules:
RewriteCond %{THE_REQUEST} \ /+\?lang=([^&]+)&([^=&\ ]+)=([^&\ ]+)
RewriteRule ^ /%1/%2/%3? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /?lang=$1&$2=$3 [L,QSA]