mod_rewrite removes the ?page= fine, get &id= not working - .htaccess

I got my .htaccess working that it is correctly going from:
webshop/index.php?page=home to webshop/home
If I put the id in the URL it still needs to be webshop/product&id=1. What I want to create is webshop/product/1.
I searched a lot of options on the internet but could not get it working. The code below is my .htaccess file.
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Removes index.php from URL
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]
# Rewrites /home to be /index.php?page=home
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^webshop/([^/]*)$ /webshop/?page=$1 [QSA]
Hope you guys can help me out.

You can use:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Removes index.php from URL
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Rewrites /webshop/product/1 to be /webshop/index.php?page=product&id=1
RewriteRule ^webshop/([^/]+)/(\d+)/?$ /webshop/?page=$1&id=$2 [NC,QSA,L]
# Rewrites /webshop/home to be /webshop/index.php?page=home
RewriteRule ^webshop/([^/]*)$ /webshop/?page=$1 [NC,QSA,L]
You can change (\d+) to ([^/]+) if your id is not only a number

Related

htaccess rewrite rule, url doesn't work when i add a / at the end and it show error

Options +FollowSymLinks -MultiViews -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{HTTP_HOST} ^themobilesapp.com$ [NC]
RewriteRule (.*) https://www.themobilesapp.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([A-Za-z0-9-]+)$ specification.php?url=$1 [L]
ErrorDocument 404 404error.php
</IfModule>**strong text**
When I open the URL https://www.themobilesapp.com/Motorola-Moto-G4-Play-specifications-7443
it works fine but when i tried to open the URL by adding "/" in the last on the url like https://www.themobilesapp.com/Motorola-Moto-G4-Play-specifications-7443/
I have tried many but it doesn't solve my problem.
I want my URL as https://www.themobilesapp.com/Motorola-Moto-G4-Play-specifications-7443
With your shown samples, please try following Rules in your .htaccess file. Please make sure to clear your browser cache before testing your URLs.
Options +FollowSymLinks -MultiViews -Indexes
DirectorySlash Off
<IfModule mod_rewrite.c>
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{HTTP_HOST} ^themobilesapp\.com$ [NC]
RewriteRule ^(.*)$ https://www.themobilesapp.com/$1 [NE,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/?$ $1.php
RewriteRule ^([\w-]+)$ specification.php?url=$1 [L]
ErrorDocument 404 404error.php
</IfModule>
Also I saw in your previous edit that you need rule like rewriting to given php files with uri, you could cover all those rules with following single rule here:
RewriteRule ^([^/]*)/([\w-]+)/?$ $1.php?url=$2 [L]
This problem has been solved using the below code.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Url rewriting in php with multiple query string variables using .htacces

I have dynamic pages in php like:
http://www.example.com/page.php?tokenid=1&tokenname=About Us
I want to remove this part:
.php?tokenid=1&tokenname=About Us
page extension with query string and show the url as:
http://www.example.com/About Us
Update:
What I've tried so far:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
#RewriteRule ^([a-zA-Z0-9\._-]*)?/(.*)$ $1.php [QSA,E=PATH_INFO:/$2,L]
RewriteRule ^([a-zA-Z0-9\._-]*)?/(.*)$ $1.php/$2 [QSA,E=PATH_INFO:/$2,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]
# Fix PHP Authentication with FastCGI mode
RewriteCond %{HTTP:Authorization} !''
RewriteRule .*php - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
</IfModule>
Try this in your .htaccess file:
RewriteEngine on
RewriteBase /
# Skip actual files/directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If the host matches example.com
RewriteCond %{HTTP_HOST} ^www\.example\.com$
# And the request URI starts with page.php
RewriteCond %{REQUEST_URI} ^\/page\.php
# And the querystring matches tokenid=DIGIT&tokenname=TOKEN_NAME
RewriteCond %{QUERY_STRING} tokenid=\d&tokenname=(.*)
# Then rewrite the URI to TOKEN_NAME
RewriteRule ^(.*)$ %1? [L,R]
You might want to try it online, if you wish.

.htaccess URL redirection using User Agent

I've been trying to find a solution to my situation but none worked. I have the following htaccess file which does the following:
It redirects site.com/m/page to site.com/m/#page
I redirects site.com/d/page to site.com/m/#page
It deletes .php extension and the common_ pattern which I had in front of the pages
The code so far:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^[dm]/(.+)$ /m/#$1 [R=302,NE,L,NC]
## hide .php extension
RewriteCond %{THE_REQUEST} \s/+common_([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]
## To internally forward
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/common_$1.php -f
RewriteRule ^(.+?)/?$ common_$1.php [L]
I would like the folowing:
a) If some user enters from an iPhone 'site.com/m/...' , continue (do nothing)
b) If some user enters from a desktop ar iPad 'site.com/m/page' to redirect him to 'site.com/d/#page'
I would really appreciate some help.
You can use a new rule for this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} !iphone [NC]
RewriteRule ^m/(.+)$ /d/#$1 [R=302,NE,L,NC]
RewriteRule ^[dm]/(.+)$ /m/#$1 [R=302,NE,L,NC]
## hide .php extension
RewriteCond %{THE_REQUEST} \s/+common_([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]
## To internally forward
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/common_$1.php -f
RewriteRule ^(.+?)/?$ common_$1.php [L]

.htaccess pretty URL's excluding index.php and adding trailing / to the end

Ok, I've fought with it for hours. I have 3 different .htaccess scripts which do what I need, but I'm unable to mix them together.
Make a pretty url from (example.com/gallery.php -> example.com/gallery)
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ $1.php
The script from #1 though forwards example.com/index.php to example.com/index, so this code removes index.php so example.com/index.php -> example.com
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
The script should add a trailing slash so example.com/gallery -> example.com/gallery/
# invoke rewrite engine
RewriteEngine On
RewriteBase /~new/
# add trailing slash if missing
rewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ $1/ [NC,R=301,L]
Can someone help me to combine those 3 scripts into one universal pretty URL scripts
add these directives to .htaccess in the root directory of your website
Options +FollowSymLinks
RewriteEngine On
# add trailing slash
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
# rewrite gallery/ to gallery.php
RewriteCond %{REQUEST_URI} /$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1.php [L]
# redirect example.com/index.php to example.com/
RewriteCond %{REQUEST_URI} index\.php$
RewriteRule ^(.*)index\.php$ /$1/ [R=301,L]

.htaccess redirect according to url

In my hosting account, I have domains. One is located at the root and the other is in the /example/ folder. Here is the redirect code in my main .htaccess file, which works fine apart from the function I just described. This function was working previously, and has mysteriously stopped-
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^/example(.*)$ http://example.com/$1 [R=301,L]
# Rewrite /example to http://example.com
# Remove .php from file names and force added slash
# http://stackoverflow.com/questions/1068595/htaccess-code-to-remove-extension-and-addforce-trailing-slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
Any ideas?
Try removing the leading slash, like this:
RewriteRule ^example(.*)$ http://example.com/$1 [R=301,L]

Resources