how can i manage url using .htaccess in php? - php

i want to manage url of my website
right now its showing me www.computermall.co.in/product_details.php?prdid=34
but i want www.computermall.co.in/product_details/34
how can i do it?
i have tried this
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /computermall
# Get the URI-path directly from THE_REQUEST variable
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\s/(.*)\.php [NC]
# Strip the extension and redirect permanently
RewriteRule .* /%2 [R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.php [NC]
# Map internally to the original resource
RewriteRule ^(.*)$ $1.php [L,QSA]

Try this:
RewriteEngine On
RewriteRule ^product_details/prdid/([^/]*)$ /product_details?prdid=$1 [L]
Edit: removed leading slash

First of all your RewriteBase /computermall is confusing but i am assuming thats there because your domain is in a sub directory of another active domain/http server path.
Here is your htaccess
RewriteBase /
RewriteRule ^computermall/product_details/([^/]*)/([^/]*$ /computermall/product_details?prdid=$2 [R=301,L]

Related

Friendly Url don't get the link [duplicate]

I am just new to .htaccess.
I need some rewrite rules for URLs.
I Google'd some and applied but no change in URL.
I want:
demo.example.com/section.php?id=1
Changed to:
demo.example.com/section/sample-section
i tried
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^section/(\d+)*$ ./section.php?id=$1
but no difference
Thanks.
I will appreciate your help.
First, make sure mod_rewrite is enabled and htaccess files allowed in your Apache configuration.
Then, put this code in your htaccess (which has to be in root folder)
Options -MultiViews
RewriteEngine On
# redirect "/section.php?id=xxx" to "/section/xxx"
RewriteCond %{THE_REQUEST} \s/section\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /section/%1? [R=301,L]
# internally rewrite "/section/xxx" to "/section.php?id=xxx"
RewriteRule ^section/([0-9]+)$ /section.php?id=$1 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^section/([^/]+)$ /section.php?id=$1 [L]
This will turn example.com/section.php?id=X to example.com/section/X
I suggest storing the URI in a database then using section.php?uri=
For example:
example.com/section.php?uri=super-awesome
would turn into:
example.com/section/super-awesome

PHP redirect to another page with dynamic url and get data [duplicate]

I am just new to .htaccess.
I need some rewrite rules for URLs.
I Google'd some and applied but no change in URL.
I want:
demo.example.com/section.php?id=1
Changed to:
demo.example.com/section/sample-section
i tried
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^section/(\d+)*$ ./section.php?id=$1
but no difference
Thanks.
I will appreciate your help.
First, make sure mod_rewrite is enabled and htaccess files allowed in your Apache configuration.
Then, put this code in your htaccess (which has to be in root folder)
Options -MultiViews
RewriteEngine On
# redirect "/section.php?id=xxx" to "/section/xxx"
RewriteCond %{THE_REQUEST} \s/section\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /section/%1? [R=301,L]
# internally rewrite "/section/xxx" to "/section.php?id=xxx"
RewriteRule ^section/([0-9]+)$ /section.php?id=$1 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^section/([^/]+)$ /section.php?id=$1 [L]
This will turn example.com/section.php?id=X to example.com/section/X
I suggest storing the URI in a database then using section.php?uri=
For example:
example.com/section.php?uri=super-awesome
would turn into:
example.com/section/super-awesome

How to send get request with forward slash instead of question mark?

I am working on a PHP application, in which I need to use forward slash in place of question mark in get request. Like:
www.example.com/article?aid=10&aname=my-article
should be changed to
www.example.com/article/10/my-article
Following is my .htaccess:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^articles/(.*)$ articles?q=$1 [L,QSA,B]
I have tried a lot, but not able to find solution. How can this can be done?
You can use this rule to rewrite /article/foo/bar to /article?aid=foo&aname=bar
Add the following right bellow RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^article/([^/]*)/([^/]+)/?$ /article?aid=$1&aname=$2 [L,QSA,B]
You can do something like that
RewriteRule ([^/\.]+)/([^/\.]+)/?$ page.php?level1=$1&level2=$2 [L]
The level then describe your full url separated by the symbol /
Then in the page hypothetically called page.php you can do:
$firstParameter = $_REQUEST["level1"];
In your case you can do:
RewriteRule article/([^/\.]+)/([^/\.]+)/?$ article?aid=$1&aname=$2 [L]
Then your url will be /article/foo/bar
N.B.
I normally use RewriteBase / to have the basic rewrite with /,
It is' to put so:
Options +FollowSymLinks
RewriteBase /
RewriteEngine On

How can I use mod_rewrite to remove the query string from my URL?

I am new at mod_rewrite and need some help if is possible.
this is my old url
www.mysite.com/web/page.php?c=categoryname
I need to change it at:
www.mysite.com/web/page/categoryname.html
I am using this code but it doesn't work:
RewriteEngine on
RewriteBase /web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^/]+)/?$ page.php?c=$1 [L]
How can I fix the above code to correctly rewrite my URLs?
EDIT:
Thanks to Ulrich Palha
i manage to redirect the html files, but css,js etc are not redirect and the website looks rly bad, this is the code i am using now, till i will get the one who will redirect css too
RewriteEngine On
RewriteBase /web/
#redirect www.mysite.com/web/page.php?c=categoryname to
#www.mysite.com/web/page/categoryname.html
#prevent internal redirect
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} (^|&)c=([^&]+)(&|$) [NC]
RewriteRule ^(page)\.php$ $1/%2.html? [NC,L,R=301]
#rewrite www.mysite.com/web/page/categoryname.html to
#www.mysite.com/web/page.php?c=categoryname
RewriteRule ^(page)/([-a-zA-Z0-9]+)\.html$ $1.php?c=$2 [L,NC]
BUMP ANY HELP PLEASE?
Add the following to the .htaccess file in the web directory of your site, replacing the rules you have above.
RewriteEngine On
RewriteBase /web/
#do not process css/js etc
RewriteCond %{REQUEST_URI} \.(css|js|jpg|png) [NC]
#alternatively you could skip all existing files. Comment previous line and uncomment next to do so
#RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
#redirect www.mysite.com/web/page.php?c=categoryname to
#www.mysite.com/web/page/categoryname.html
#prevent internal redirect
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} (^|&)c=([^&]+)(&|$) [NC]
RewriteRule ^(page)\.php$ $1/%2.html? [NC,L,R=301]
#rewrite www.mysite.com/web/page/categoryname.html to
#www.mysite.com/web/page.php?c=categoryname
RewriteRule ^(page)/([-a-zA-Z0-9]+)\.html$ $1.php?c=$2 [L,NC]
Your regex in the rule is wrong — you are missing /page, adding an optional final slash that you shouldn't be, and forgetting to remove the .html part. Also your RewriteBase shouldn't have a final slash and your rule shouldn't have a leading slash.
It total, it should be this:
RewriteEngine on
RewriteBase /web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([^/]+)\.html$ page.php?c=$1 [L]

htaccess add trailing slash and force www with clean urls

I'm using my htaccess file with mod_rewrite to create clean urls like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
I would also like to force the site to have the 'www' subdomain and most importunately add a trailing slash if the url doesn't have one.
I am an absolute noob with mod_rewrite and I've tried accomplishing this on my own by combining other code I found on google (sad I know), but I always end up with a 500 error.
Here's the code I found for force www:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.domain\.tld$ [NC]
RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]
</IfModule>
Thanks for your help.
Try separating out the www and the trailing slash check. This is tested and hopefully working for you. You didn't say if you're running placing at domain root or in a subdirectory - usually good info when asking for help with htaccess.
RewriteEngine On
# Assuming you're running at domain root. Change to working directory if needed.
RewriteBase /
#
# www check
# If you're running in a subdirectory, then you'll need to add that in
# to the redirected url (http://www.mydomain.com/subdirectory/$1
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
#
# Trailing slash check
# Don't fix direct file links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
#
# Finally, forward everything to your front-controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [QSA,L]
To debug, comment out the individual sections and see what is/isn't working.
Use this and forgot your problems ;)
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*?)/*$ http://%1/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://your-domain.ru/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

Categories