htaccess how to short url with get - apache

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.

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]

RewriteRule (contact.php -> contact) + 404 Redirection gives me always a 404 page

Recently i Modified my .htaccess file in my apache server. and this is the following code
RewriteEngine On
<IfModule mod_speling.c>
CheckSpelling On
</IfModule>
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^course/([^/.]+)$ course.php?name=$1 [QSA,L]
RewriteRule ^course/([^/.]+)/$ course.php?name=$1 [QSA,L]
RewriteRule ^about$ about.php [QSA,L]
RewriteRule ^about/$ about.php [QSA,L]
RewriteRule ^contact$ contact.php [QSA,L]
RewriteRule ^contact/$ contact.php [QSA,L]
RewriteRule ^downloads$ downloads.php [QSA,L]
RewriteRule ^downloads/$ downloads.php [QSA,L]
RewriteRule ^ errors/404.html [L,NC]
Now the Problem is
If I I enter the URL like http://localhost/about in my web browser it well always shows the custom 404.html
I think the problem is on the last line.
If i remove the last line it will works.
But I also want the 404 page. Like If the user enters http://localhost/SomeRandomString the 404 page show on this url.
I also tried ErrorDocument on .htaccess file but it will change the url
Is any solution for this problem.
Have your htaccess rules file in following way. Place it in your root directory, make sure to clear your browser cache before checking your URLs.
RewriteEngine On
<IfModule mod_speling.c>
CheckSpelling On
</IfModule>
##Rules that cover your uris starting with course here.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(course)/([^/.]+)/?$ $1.php?name=$2 [NC,QSA,L]
##Rules that cover your uris starting with about/contact/downloads here.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(about|contact|downloads)/?$ $1.php [NC,QSA,L]
##Rules that cover all uris which aren't matching any of the above conditions.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ errors/404.html [L,NC,QSA]

Joomla: Permanently redirect from example.com/index.php/foo/bar to example.com/foo/bar (no index.php in URL)

.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
With this setup the page /foo/bar is accessible under the pretty URL example.com/foo/bar while the ugly URL example.com/index.php/foo/bar is still valid. What I need to achieve is a permanent redirection from example.com/index.php/foo/bar to example.com/foo/bar.
RewriteRule .* index.php [R=301,L] doesn't work.
RewriteRule (.*) index.php/$1 [R=301,L] does the exact opposite, it redirects from example.com/foo/bar to example.com/index.php/foo/bar. Please help me out!
I recommend writing this:
RewriteEngine On
RewriteBase /
# remove index.php from / OR any /dir
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
# redirect rule to remove /index.php/ from start
RewriteRule ^index\.php(/.*) $1 [L,R=301,NC]
RewriteCond %{REQUEST_URI} !^/index\.php [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
In the new rule, we match a pattern using regex ^index\.php(/.*) which matches a URI that starts with /index.php/ followed by zero or more of any characters. (/.*) is our match and the capture group after /index.php which is used as back-reference i.e. $1 (string that we've captured in group #1) in target to get a URI after /index.php as desired.
References:
Apache mod_rewrite Introduction
.htaccess tips and tricks

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]

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]