Customize Url or Hide Query String using .htaccess - php

I am new one, I learn .htaccess. I want to customize my URL
from
index.php?page=mobile
to
index/page/mobile
I used this code but it doesn't work:
RewriteEngine on
RewriteRule ^index/([0-9]+)/?$ index.php?page=$1 [NC,L] # Handle product requests
and
RewriteEngine on
RewriteRule ^index/([^/.]+)/?$ index.php?page=$1 [L]

This rule should work:
RewriteEngine on
RewriteBase /cashearn/
RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ index/page/%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^index/page/([^/]+)/?$ index.php?page=$1 [L,QSA,NC]
This will let you have URL as http://domain.com/index/page/mobile on your page.

Try that code:
RewriteEngine on
RewriteBase /
RewriteRule ^index/page/([^/.]+)/?$ index.php?page=$1 [L,QSA,NC]

This should work - not tested though
RewriteRule ^index/page/([^/.]+)$ index.php?page=$1 [L,QSA,NC]

Related

Rewrite rule not working correctly

I have been trying to apply a rewrite rule to change my urls which currently look like this:
www.mysite.com/my-site/en
change to:
www.my-site.com/en or /es
but I want it to be forceful and not allow someone to deliberately type in the latter.
Also
www.my-site.com/en/?page=car&category=15
change to
www.my-site.com/en/car/15 (or I can just put the category name instead)
I also have this:
www.my-site.com/en/?page=catalogue-item&category=204&id=563
which I want to change to:
www.my-site.com/en/563 (or I can just put in the item name instead)
Here is my htaccess so far:
# RewriteOptions inherit
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^my-site.com [NC]
RewriteRule ^(.*)$ http://www.my-site.com/$1 [L,R=301,NC]
RewriteRule ^$ my-site/ [L,NS,R=301]
RewriteCond %{HTTP_HOST} ^www.my-site.com/my\-site/ [NC]
RewriteRule ^(.*)$ http://www.my-site.com/$1 [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ my-site/$1 [L,NS]
RewriteRule ^([^/]*)$ /en/?page=$1 [L]
RewriteRule ^([^/]*)$ /es/?page=$1 [L]
RewriteRule ^([^/]*)/([^/]*)$ /en/?page=$1&category=$2 [L]
RewriteRule ^/([^/]+)/~([^/]+)/(.*)$ my-site/$1 [L,NS]
</IfModule>
I thank you in advance and hope someone could point out my problem, thank you again.

Slash after file extension can not be redirected

I am using these htaccess code to remove and redirect php extension to without file extension.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
Then I try to use another slightly different code
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.[a-zA-Z0-9]+$ $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Both code produce same problem, if try to access
domain.com/other/content.php goes to domain.com/other/content (200 response)
Now here is strange behaviour, if try to access
domain.com/other/content.php/ goes to domain.com/other/content.php/ (200 response not 404)
domain.com/other/content.php/any-word goes to domain.com/other/content.php/any-word (200 response not 404 error)
All other redirect work perfectly, besides this problem.
Can any one help with both code, I need both code rectification, as I have two different site with both htaccess rule.
If I keep both code
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
Plus, I add new code then it works
RewriteEngine On
RewriteCond %{THE_REQUEST} /([^.]+)\.php/? [NC]
RewriteRule ^ /%1/ [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /$1.php [L,NC]

rewrite url www.xyz.com/index.php?id=xyz to www.xyz.com/xyz using htaccess

I want to include item details details.php if url has parameter id=xyz
rewrite
url www.example.com/index.php?id=xyz
to
www.example.com/xyz.html
using htaccess
htaccess i am using has
RewriteEngine On
RewriteBase /
RewriteRule ^([^.]+)\.html$ index.php?id=$1 [L,QSA,NC]
RewriteCond %{THE_REQUEST} \s/index\.php\?id=([_0-9a-zA-Z-]+)\s [NC]
RewriteRule ^%1? [R=301,L]
RewriteRule ^([_0-9a-zA-Z-]+)$ /index.php?id=$1 [L]
RewriteCond %{THE_REQUEST} \s/index\.php\?id=([_0-9a-zA-Z-]+)\s [NC]
RewriteRule ^ %1.html? [R=301,L]
RewriteRule ^([_0-9a-zA-Z-]+?)(?:\.html)?$ /index.php?id=$1 [NC,L]
worked perfect
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^([^.]+)\.html$ index.php?id=$1 [L,QSA,NC]
Use this .htaccess to rewrite:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)\.html$ index.php?id=$1 [L,QSA,NC]
</IfModule>
Make sure you have mod_rewrite enabled (use phpinfo() or apache_get_modules() in PHP if available)
Please try following configuration in your www.example.com/.htaccess file,
i just test it and work fun on my env.
//htaccess file content
RewriteEngine On
RewriteRule ^(\w+).html$ /index.php?id=$1 [L]

Using htaccess to rewrite url with variables?

Using htaccess how can instead of the following to have something like
http://domain.com/verify
or
http://domain.com/verify?user=_FIRST-VARIABLE_&verification_code=_SECOND-VARIABLE_
Here is the original
http://domain.com/account/index.php?user=_FIRST-VARIABLE_&verification_code=_SECOND-VARIABLE_
My current htaccess is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^login$ account/index.php [NC]
RewriteRule ^register$ account/index.php?register [NC]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Try this RewriteRule with the regex pattern
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^varify/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$/account/index.php?foo=$1&bar=$2 [NC,QSA,L]
change the variables foo and bar with your variables.
All requests to example.com/varify/var1/var2 will be redirected to example.com/account/index.php?foo=$1&bar=$2

How to delete a subdirectory from a URL?

So I'm trying to turn my website from this:
http://profe5.com/Profe5-Web/public_html/login.html
To this:
http://profe5.com/login
I've been struggling to do this, but whenever I run it I get 404 error!
This is my htaccess:
DirectoryIndex Profe5-Web
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^profe5\.com$ [NC]
RewriteRule ^(.*)$ http://profe5.com/$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [R=301,L]
RewriteRule ^Profe5-Web/public_html/(.*)$ $1 [R=301,L]
It would be so awesome if you guys could help me!
Thanks so much!
This should be your complete .htaccess:
DirectoryIndex Profe5-Web
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^profe5\.com$ [NC]
RewriteRule ^(.*)$ http://profe5.com/$1 [R=301,L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+Profe5-Web/public_html/([^.]+)\.html [NC]
RewriteRule ^ %1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ Profe5-Web/public_html/$1.html [L,QSA]
you might consider instead doing this via a virtual host; check with your hosting company about setting it up
If you want /login to map to Profe5-Web/public_html/login.html - try this:
RewriteRule ^([^.]+)$ Profe5-Web/public_html/$1.html [R=301,L]
The RewriteRule you already have, RewriteRule ^([^\.]+)$ $1.html [R=301,L] will map /login to /login.html, which doesn't seem to do what you need it to.

Categories