Issue redirecting query string to path - apache

I want to redirect a get query parameter to a path and maintain the ability to use the GET parameter in the code.
I struggle to find examples excluding the file extension so perhaps this is what is throwing me.
I have tried the below code but not getting expected results from it?
RewriteCond %{REQUEST_URI} ^/spares/$
RewriteCond %{QUERY_STRING} pid=([0-9]*)$
RewriteRule ^(.*)$ /spares/%1 [R=302,L]
For example:
mysite.com/spares/?pid=x
to
mysite.com/spares/x
EDIT:
Copy of .htaccess in /dev/ subdirectory.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /dev/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /dev/index.php [L]
</IfModule>
RewriteEngine On
# To externally redirect with pid
RewriteCond %{THE_REQUEST} \s+spares/\?pid=(.*)\s [NC]
RewriteRule ^ /spares/%1 [R=301,L,NE]
# To internally rewrite to ?pid=
RewriteCond %{QUERY_STRING} pid=(.+)$
RewriteRule ^dev/spares/$ /dev/spares/?pid=%1 [NC,L]
# END WordPress

Try with:
RewriteEngine On
# To externally redirect with pid
RewriteCond %{THE_REQUEST} \s/+spares/\?pid=(.*)\s [NC]
RewriteRule ^ /spares/%1 [R=301,L,NE]
# To internally rewrite to ?pid=
RewriteCond %{QUERY_STRING} pid=(.+)$
RewriteRule ^spares/$ /spares/?pid=%1 [NC,L]

Related

Long URL in php using htaccess

I have a very long URL: //domain/administration/registrar/term_detail.php?a_id=47
And I want to make it looks like: //domain/administration/registrar/detail/47
I have created an htaccess file in //domain/administration/registrar/. Below is my code in htaccess and it is not working. Any suggestion of what I did wrong is appreciated.
Both of my htaccess and term_detail.php files are inside of registrar folder.
RewriteEngine On
RewriteBase /administration/registrar/
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} \s/+registrar/detail\.php\?a_id=([^\s&]+) [NC]
RewriteRule ^ administration/registrar/%1? [R=301,L]
RewriteRule ^administration/registrar/([a-zA-Z0-9]+)/?$ detail?a_id=$1 [L,QSA,NC]
You can try these rules inside your /administration/registrar/.htaccess:
RewriteEngine On
RewriteBase /administration/registrar/
## External redirect to /administration/registrar/term_detail/47
RewriteCond %{THE_REQUEST} /([^./]+)(?:\.php)?\?a_id=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L,NE]
## Internal rewrite to term_detail.php?a_id=47
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?a_id=$2 [QSA,L]
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
Keep your htaccess file inside registrar folder.
RewriteEngine ON
##External redirect to //domain/administration/registrar/detail/47 URL.
RewriteBase /administration/registrar/
RewriteCond %{THE_REQUEST} \s/([^/]*)/([^/]*)/(?:[^_]*)_([^.]*)\.php\?a_id=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=301,L]
##Internal rewrite to term_detail.php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ $1/$2/term_$3.php?a_id=$4 [QSA,L]

htaccess - remove index.php and keep a variable without key

original url is one of the following:
example.com
example.com/index.php
example.com/index.php?c=lorem
if example.com - nothing should be done
if example.com/index.php - it should be visible as example.com and here is my code - works fine:
RewriteEngine ON
RewriteRule ^index\.php/?$ https://%{HTTP_HOST}/ [NC,R=301]
RewriteRule ^/?$ index.php [L]
if example.com/index.php?c=lorem it should be visible as example.com/lorem
and lorem should be accessible by php in both cases:
if a user type example.com/index.php?c=lorem or
a user type example.com/lorem
pls help
Could you please try following, based on your shown samples.
Please do clear your browser cache before testing your URLs.
RewriteEngine ON
##This rule is for handling index.php file in uri.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php/?$ / [R=301,NC,L]
##This rule is to redirect from index.php?c=lorem to lorem in uri.
RewriteCond %{THE_REQUEST} index\.php\?c=(lorem)\s [NC]
RewriteRule ^ http://%{HTTP_HOST}/%1? [R=301,L,NE]
##Rule for non-existing files/directories to index.php with variables.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?c=$1 [L]
OR: Above is for specifically lorem in case your query string could be anything then try following.
RewriteEngine ON
##This rule is for handling index.php file in uri.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php/?$ / [R=301,NC,L]
##This rule is to redirect from index.php?c=lorem to lorem in uri.
RewriteCond %{THE_REQUEST} index\.php\?c=([^\s&]*) [NC]
RewriteRule ^ http://%{HTTP_HOST}/%1? [R=301,L,NE]
##Rule for non-existing files/directories to index.php with variables.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?c=$1 [L]
You may use these rules in your site root .htaccess:
DirectoryIndex index.php
RewriteEngine On
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php\s [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+index\.php\?c=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ index.php?c=$1 [L,QSA]

How can I use .htaccess to hide extension and prevent user access with extension url?

here is my code for hide .html and .php extension when user view my site, http://example.com/xxx.html to http://example.com/xxx
But I hope to make user when they input http://example.com/xxx.html, they cannnot access and jump to 404 not found.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# Remove .html-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)/$ $1.html
# End of Apache Rewrite Rules
</IfModule>
Anyone have solution?
You can add this as your first rule:
RewriteCond %{THE_REQUEST} \.(?:html|php)\s [NC]
RewriteRule ^ - [R=404,L]
Update
Perhaps this will fix your ErrorDocument problem:
RewriteCond %{THE_REQUEST} \.(?:html|php)\s [NC]
RewriteCond %{REQUEST_URI} !=/error404.html
RewriteRule ^ - [R=404,L]

Exclude directory from rewrite rule

Below is my .htaccess file. I am trying to exclude the rewrite rules being applied to the /admin/ folder, however after spending an hour researching and trying various codes I have reached a wall.
Options -MultiViews
RewriteEngine On
RewriteBase /
# Exclude admin directory
RewriteRule ^(admin|admin)($|/) - [L]
RewriteRule ^privacy$ pages/privacy.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.+)$ products.php?q=$3&rewrite=1&%{QUERY_STRING} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/$ search.php?q=bw:&categoryFilter=$1&genreFilter=$2%{QUERY_STRING} [L,B]
RewriteRule ^(.*)/$ pages/format.php?format=$1 [NC,L}
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
The above code brings an internal 500 error and if I remove it will redirect me to the rewrite rule for the format. I was reading that The exclusion rewrite rule needs to be placed before the rule I want to overwrite. I don't understand why it's not working and what I have missed.
Thanks.
Try and write your rule this way.
# Exclude admin directory
RewriteRule ^admin/?$ - [L]
You have a syntax error in your .htaccess.
Have your .htaccess like this:
Options -MultiViews
RewriteEngine On
RewriteBase /
# Exclude admin directory
RewriteRule ^admin($|/) - [L,NC]
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^privacy$ pages/privacy.php [NC,L]
RewriteRule ^(.+)/(.+)/(.+)$ products.php?q=$3&rewrite=1 [L,QSA]
RewriteRule ^(.*)/(.*)/$ search.php?q=bw:&categoryFilter=$1&genreFilter=$2 [L,B,QSA]
RewriteRule ^(.+)/$ pages/format.php?format=$1 [NC,L]

Simple URL Rewrite Problems

I'm having trouble re-writing a simple URL with .htaccess.
The URL I am trying to rewrite is:
http://www.domain.com/index.php?page=PAGE_NAME
I would like the PAGE_NAME to be directly after the domain, for example if PAGE_NAME is blog:
http://www.domain.com/blog
At the moment I have tried the following with no success:
RewriteRule ^/?([a-zA-Z0-9_-]+)?$ index.php?page=$1
All help is really appreciated.
Thanks in advance.
My current .htaccess file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
ErrorDocument 404 /search.php?page=notfound
# Add WWW to URL
RewriteCond %{HTTP_HOST} ^cristianrgreco\.com$ [NC]
RewriteRule ^(.*)$ http://www.cristianrgreco.com/$1 [L,R=301]
# Remove trailing slashes from end of URL
RewriteCond %{HTTP_HOST} !^\.cristianrgreco\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [L,R=301]
# Rewrite main page URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^\ ]+) [NC]
RewriteRule ^ %1? [L,R=301]
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1 [L,NC]
# Rewrite download URLs
RewriteRule ^download/([a-zA-Z0-9._-]+)/?$ download.php?file=$1
# Rewrite page navigation links
RewriteRule ^(.+?)/page-([a-zA-Z0-9_-]+)?$ $1.php?currentpage=$2
# Rewrite article URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ articles.php?article=$2
# Remove file extension from PHP files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L,QSA]
</IfModule>
Try adding the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
#add these next 2 lines if you need to redirect http://www.domain.com/index.php?page=PAGE_NAME to http://www.domain.com/PAGE_NAME
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^\ ]+) [NC]
RewriteRule ^ %1? [L,R=301]
#send request to index.php
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1 [L,NC]