Long URL in php using htaccess - apache

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]

Related

Rewrite URLs in .htaccess for replacing Query parameters with forward slash (id?=value)

I have made sure that rewrite engine is enabled and removing .php extensions is working so I know that isn't the issue.
what I'm trying to do is simply remove the ?id=value aspect of the URL, so basically making the URL look like such:
folder/medias/value
Instead of
folder/medias?id=value
My current .htaccess looks like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^404/?$ /404.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ 404.php [L,R]
With your shown samples/attempts, please try following htaccess Rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules for external rewrite.
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?id=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Rule for internal rewrite.
RewriteRule ^([^/]*)/([^/]*)/?$ $1?id=$3 [L]
You may try this code inside the /folder/.htaccess (create this file if it doesn't exist):
RewriteEngine On
# External redirect from /folder/media?id=val to /folder/media/val
RewriteCond %{THE_REQUEST} /(\S+?)\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ /folder/%1/%2? [R=301,L,NE]
# Internal rewrite from /folder/media/val to /folder/media?id=val
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?id=$2 [L,QSA]
Trailing ? in first rule is to remove query string from original URL.
%{REQUEST_FILENAME} !-f and %{REQUEST_FILENAME} !-d is to skip existing files and directories from rewrite in 2nd rule.

htaccess how to short url with get

I want to short my url from this:
https://example.com/?page=home
to
https://example.com/home
but I get this error:
Not Found
The requested URL was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
My index.php is on the folder: /
My pages like home.php etc are on the folder: /pages
My code
RewriteEngine On
RewriteBase /
# Prevent looping
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
# Rewrite if not index.php page
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]
# Change url from page={something} to {something} and redirect
RewriteCond %{REQUEST_URI} /index.php
RewriteCond %{QUERY_STRING} page=(.*)
RewriteRule . %1? [L,R]
File-structur
/
-index.php (File)
-.htaccess (File)
-Images (Folder)
-Css (Folder)
-Pages (Folder)
--Home.php
1st solution: Based on your shown samples, could you please try following. This one considers that you are hitting url https://example.com/home in browser.
RewriteEngine ON
Options -MultiViews
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?page=$1 [L]
2nd solution: OR in case you are hitting https://example.com/?page=home in your browser then try following.
RewriteEngine ON
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^/?$ https://%{HTTP_HOST}/%{QUERY_STRING} [R=301]
RewriteRule ^(.*)/?$ index.php?page=$1 [L]
NOTE: Please make sure you are putting only 1 of the rules set at a time, as they are written for different purposes.

Issue redirecting query string to path

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]

htaccess conflicting with MediaWiki htaccess

I have the following code in my htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^wiki/(.*)$ /index.php?title=$1 [PT,L,QSA]
RewriteRule ^wiki/*$ /index.php [L,QSA]
RewriteRule ^wiki$ /index.php [L,QSA]
This shorten the URL from http://example.com/w/index.php?title=Page_title to example.com/wiki/Page_title.
I would also like to redirect example.com to www.example.com, but I am not sure on how I should implement this into the existing htaccess code without conflicting with other rules.
How can this be done?
RIght under RewriteBase /, add:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]

url rewrite for subfolder

I'm trying to do rewrite only for a subdirectory, but I have a problem with RewriteCond.
All my files are in /abc.
Here is my htaccess (/abc/.htaccess) file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/static/ [NC,OR]
RewriteCond %{REQUEST_URI} ^/files/ [NC]
RewriteRule . - [L]
RewriteRule (.*) index.php?_url=$1 [QSA,L]
So I want to rewrite every url to index.php except the existing files in /static and /files. But this code is rewriting /static/a.jpg too, since the request was /abc/static/a.jpg. I tried setting RewriteBase to /abc/, but nothing changed.
Any idea how can I solve this?
Have your .htaccess like this in /abc/.htaccess:
RewriteEngine on
RewriteBase /abc/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_URI} ^/(static|files)(/|$) [NC]
RewriteRule . - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?_url=$1 [QSA,L]