I have url localhost/product/q.php?id=1&product=my-product
how to make the url into localhost/product/1/my-product using .htaccess
You use a RewriteRule which consists of a regex to apply to the pretty URL accessed by the user and a replacement which will then be used by the server/scripts.
RewriteRule ^product/(\d+)/(.*)$ /product/q.php?id=$1&product=$2 [L]
RewriteRule : Type of action
^product/(\d+)/(.*)$ : Regex to run against URL
/product/q.php?id=$1&product=$2 : Replacement URL
[L] : Flags; L == Last
Note
Having re-read the question you may need to add:
Options -MultiViews
To the top of your .htaccess file as well.
Related
My present URL is :
https://www.example.com/urlvalue1/urlvalue2/urlvalue3/urlvalue4/urlvalue5
I need to remove urlvalue3/urlvalue4/ from my URL all over the website.
How can I achieve this using .htaccess ?
presently in .htaccess I am using :
RewriteRule ^((!/index/cslug/).+)$ /index/cslug/$1 [L,NC]
The regex where you have (! should instead be: (?! for it to work as a negative lookahead:
RewriteRule ^((?!/index/cslug/).+)$ /index/cslug/$1 [L,NC]
next, to replace a 5 part URL:
RewriteRule ^([^/]+)/([^/]+)/[^/]+/[^/]+/([^/]+)/?$ /$1/$2/$3 [R,L]
I have duplicate urls and i need to do some redirection in htaccess
Examples:
Original Urls are :
www.mywebsite.com/listofgames/pacman.html
www.mywebsite.com/listofgames/nintendo.html
www.mywebsite.com/listofgames/snake.html
I have about 1000 games name
Example of duplicate urls :
www.mywebsite.com/listofgames/1/pacman.html
www.mywebsite.com/listofgames/1521/pacman.html
www.mywebsite.com/listofgames/52154/pacman.html
www.mywebsite.com/listofgames/abc/pacman.html
www.mywebsite.com/listofgames/opq/pacman.html
www.mywebsite.com/listofgames/opq/1/2/35/pacman.html
www.mywebsite.com/listofgames/154/3562/snake.html
www.mywebsite.com/listofgames/2541/snake.html
www.mywebsite.com/listofgames/524/snake.html
www.mywebsite.com/listofgames/absc/gtar/15/nintendo.html
www.mywebsite.com/listofgames/nije/nintendo.html
I need to redirect them to original urls
So
www.mywebsite.com/listofgames/anynumber/mygamesname.html
www.mywebsite.com/listofgames/anyword/mygamesname.html
www.mywebsite.com/listofgames/anyword/anynumber/anyword/mygamesname.html
Must be redirected to
www.mywebsite.com/listofgames/mygamesname.html
You may try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/listofgames/.*/([^\.]+)\.html/? [NC]
RewriteRule .* listofgames/%1.html [R=301,L]
Redirects permanently
http://www.mywebsite.com/listofgames/any/number/of/folders/mygamesname.html
with or without trailing slash.
To:
http://www.mywebsite.com/listofgames/mygamesname.html
Effectively removing the segment path between /listofgames/ folder and the HTML file.
String listofgames and extension html are assumed to be fixed, while mygamesname is assumed to be variable.
The incoming URL structure has to be kept for the rule-set to work: Last string inside the URL must be the HTML file.
For silent mapping, remove R=301 from [R=301,L]
I have the following url:
http://example.com/folder/index.php?module=centrale&DIO0=1
I need this page to be availeble with the following URL format:
http://example.com/folder/centrale/?DIO0=1
But how do i rewrite this using htaccess properly? What i've got so far:
RewriteEngine On
RewriteRule ^centrale/(.*)$ /index.php?module=centrale ????? $1
Notice that the DIO0 parameter is not always the only one, so the query string must be completly passed trough. Thanks in advance for helping me out or setting me in the right direction.
Edit: the htaccess file is located here: http://example.com/folder/
Try this :
RewriteEngine on
RewriteBase /folder/
RewriteRule ^centrale/$ index.php?module=centrale [L,QSA]
the [QSA] flag (Query String Append) will add the rest of the parameters automatically
I'm trying to come up with some mod_rewrite to translate http://example.com/?7gudznrxdnu into http://example.com/view.php?id=7gudznrxdnu
But any other page will function properly such as http://example.com/contact and so on.
I think this will work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^[a-z0-9]+$
RewriteRule ^$ view.php?id=%{QUERY_STRING} [L]
If you want the rewrite to be shown in the browser's address field, you'll have to replace [L] with [L,R=301].
Explanation: The query-string (what's following the question mark) is not part of the URL that RewriteRule sees in its matching-pattern, therefore you can't check for question mark there. In my solution, I run the rule if and only if (RewriteCond) the query string consists solely of a-z and/or 0-9, and my rule only rewrites URLs ending with a slash (except for the query string). I redirect this to view.php?id=, and then append the query string to that.
Edit: Tested on my Apache-server, and I haven't found any bugs (yet).
You should try (in your .htaccess):
RewriteEngine On
RewriteRule ^\?([^/\.]+)?$ view.php?id=$1 [L]
I have a news (blog) site that returns urls in the following format when individual posts are selected:
website.net/sitenews.php?q=posts/view/postname/12
I am seeking to rewrite the url so that it reads:
website.net/sitenews.php/posts/view/postname/12
or any other way where the ?q= is removed for purpose of removing the ? so that the url can be accessed by facebook's like button as the facebook url linter does not parse query strings.
In the htdocs .htaccess file in the root directory I have tried the following:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} q=
RewriteRule (.*) website.net/sitenews.php/$1? [R=301]
This successfully removes the q=? however the rest of the string (posts/view/postname/12) is not returned and the url now looks as follows:
website.net/sitenews.php/sitenews.php
Does anyone have any suggestions to help me complete this url_rewrite?
Try this instead in your .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=(.*)$ [NC]
RewriteRule ^(.*)$ /$1/%1? [R=301,L,NE]
R=301 will redirect with https status 301
L will make last rule
NE is for no escaping query string
%1 is capture group for query string q= (whatever comes after q=)
$1 is your REQUEST_URI
If you are using any CMS, like wordpress, or joomla or SE, then you have option to do that else you need to have an .htaccess file where you can write the code, refer this links
http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html
http://www.webmasterworld.com/forum92/2545.htm
http://www.google.com/#sclient=psy&hl=en&q=htaccess+change+the+url&aq=0p&aqi=p-p1g4&aql=&oq=htaccess+&pbx=1&bav=on.2,or.r_gc.r_pw.&fp=c875dd2b8adea15a