How to rewrite paged URL's? - .htaccess

I have changed my web page url from localhost/products.php to localhost/products using the code below.
RewriteCond %{THE_REQUEST} /products\.php[\s/?] [NC]
RewriteRule ^ /products [R=301,L]
RewriteRule ^products/?$ products.php [L,NC]
But when I navigate to next page, address bar shows something like:
localhost/products?page=2 which I want to appear as localhost/products/page2
Tried this RewriteRule ^products/page/([0-9]+)/?$ products.php?page=$1 [L] but this did not work.
My full code for rewriting page url is:
RewriteCond %{THE_REQUEST} /products\.php[\s/?] [NC]
RewriteRule ^ /products [R=301,L]
RewriteRule ^products/?$ products.php [L,NC]
RewriteRule ^products/page/([0-9]+)/?$ products.php?page=$1 [L]
Here how can I change the URL for next pages? I mean from localhost/products?page=2 to localhost/products/page2 and so on.

You can use the following rule :
RewriteCond %{THE_REQUEST} /products/?(?:\.php)?\?page=([^\s&]+) [NC]
RewriteRule ^ /products/%1? [R=301,L]
RewriteRule ^products/([0-9]+)/?$ /products.php?page=$1 [L,NC]
This will convert the urls
/products.php?page=number
/products/?page=number
to
/products/number

Related

rewrite and redirect URL without causing in a redirect loop

I have some URLs which i want to rewrite and redirect
mysite.com/search2.php?search=abc should be redirected to mysite.com/search/abc.
And mysite.com/search/abcshould show page mysite.com/search2.php?search=abc without changing URL.
I have written the rewrite code but can't do redirection without causing an infinite loop.
RewriteEngine On
RewriteRule ^search/([^/]+)?$ search2.php?search=$1 [QSA,NC]
RewriteRule ^search/([^/]+)/([^/]+)/([^/]+)?$ search2.php?search=$1&$2=$3 [L,QSA,NC]
RewriteRule ^search/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ search2.php?search=$1&$2=$3&$4=$5 [L,QSA,NC]
RewriteRule ^search/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ search2.php?search=$1&$2=$3&$4=$5&$6=$7 [L,QSA,NC]
RewriteRule ^file/([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ file2.php?$1=$2&$3=$4 [L,QSA,NC]
RewriteRule ^directory/([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ directory2.php?$1=$2&$3=$4 [QSA,NC]
RewriteRule ^directory/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ directory2.php?$1=$2&$3=$4&$5=$6 [L,QSA,NC]
You can use this htaccess
RewriteEngine on
RewriteCond %{THE_REQUEST} /search\.php\?search=([^\s]+)\s [NC]
RewriteRule ^ /search/%1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^./]+)/?$ /search.php?search=$1 [L]

htaccess hide subfolder in all pages

I found similar topics, but not an answer working for me.
My htaccess is actually :
RewriteEngine on
# remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?publigraphix\.com$
RewriteRule !^Jomla_344french/ /Jomla_344french%{REQUEST_URI} [L]/Jomla_344french%{REQUEST_URI} [L]
With this, I can hide the subfolder in the url of my homepage, but it appears again by opening any page of my website.
How can I hide the subfolder in the url on all pages ?
Thank You for help, this is my first post.
Greetings,
Susanne
Thanks for Your help. Now my htaccess is like this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?publigraphix.com$
RewriteCond %{THE_REQUEST} /Jomla_344french/(.*)\sHTTP [NC]
RewriteRule ^ /%1 [NE,L,R]
RewriteCond %{HTTP_HOST} ^(www.)?publigraphix.com$
RewriteRule !^Jomla_344french /Jomla_344french%{REQUEST_URI} [NC,L]
For the homepage, this works fine, but in the subpages, the url displays /index.php/ with an 404 error (for example url shows : publigraphix.com/index.php/tampons instead of publigraphix.com/tampons ...) Do You have an idea?
To hide the subfolder ,you can use
RewriteEngine on
# remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?publigraphix\.com$
RewriteCond %{THE_REQUEST} /Jomla_344french/(?:index\.php/)?(.*)\sHTTP [NC]
RewriteRule ^ /%1 [NE,L,R]
RewriteCond %{HTTP_HOST} ^(www\.)?publigraphix\.com$
RewriteRule ^((?!Jomla_344french).*) /Jomla_344french/index.php/$1 [NC,L]

Redirecting a page to another while reporting 301

I have a page at example.com/news.php. This page is already indexed by Google. I would like to ditch this page and instead use example.com/news.
So essentially I need to report 301 on news.php and also redirect any request to /news to news.php.
When I am using the below in .htaccess I am getting a redirect loop:
Redirect 301 /news.php http://example.com/news
RewriteRule ^news$ news.php [L]
How would I go about doing the desired?
Try :
RewriteEngine on
RewriteCond %{THE_REQUEST} /news\.php [NC]
RewriteRule ^ /news [L,R=301]
RewriteRule ^news/?$ news.php [L]
Edit as per your comment :
To redirect any php file , you can use :
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/?$ $1.php [L]

Redirect dynamic URL with two parameters to a static URL, htaccess

I have dynamic URL with two parameters (Dynamic values) which I want to rewrite and redirect to Static URL:
http://www.example.com/pagename.php?id=[some_number]&title=[some-title-seperated-by-hyphens]
above URL should be rewriten and redirected to below URL so that when either of the two URLs is entered in the browser address
bar it should show the below URL. And I also want that even on hover on 1st URL hyperlink it should show the static URL
on the bottom left of the browser:
http://www.example.com/[some_number]/[some-title-seperated-by-hyphens]
I have tried the below code and it does not work.
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+/pagename\.php\?id=([^&]+)&title=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteRule ^([^/]+)/(-[^/]+)/?$ /pagename.php?id=$1&title=$2 [L,QSA]
Try these rules:
RewriteEngine On
RewriteCond %{THE_REQUEST} /pagename\.php\?id=([^&\s]+)&title=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,NE,L]
RewriteRule ^(\S+)\s+(.*)$ $1-$2 [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^(\S+)$ /$1 [NE,R=302,L]
RewriteRule ^([^/]+)/([^/]+)/?$ pagename.php?id=$1&title=$2 [L,QSA]

making url rewrites dynamic

Here is an example of my url rewrites.
# Redirect /page1.php?parameter-view=value to /page1/value
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+page1\.php\?parameter-view=([^\s]+) [NC]
RewriteRule ^ /page1/%1? [R=302,L]
# Internally forward /page1/value to /page1.php?parameter-view=value
RewriteRule ^page1/(.*)/(.*)/?$ /page1.php?parameter-view=$1&parameter-id=$2 [L,QSA,NC]
# Internally forward /page1/value to /page1.php?parameter-view=value
RewriteRule ^page1/(.*)?$ /page1.php?parameter-view=$1 [L,QSA,NC]
# Redirect /page2.php?parameter-view=value to /page2/value
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+page2\.php\?parameter-view=([^\s]+) [NC]
RewriteRule ^ /page2/%1? [R=302,L]
# Internally forward /page2/value to /page2.php?parameter-view=value
RewriteRule ^page2/(.*)/(.*)/?$ /page2.php?parameter-view=$1&parameter-id=$2 [L,QSA,NC]
# Internally forward /page2/value to /page2.php?parameter-view=value
RewriteRule ^page2/(.*)?$ /page2.php?parameter-view=$1 [L,QSA,NC]
I have a set of rewrites for page1 and page2. Is there a way to combine these rewrites so everytime I make a new page I can just add the page "page3" instead of adding a 3rd group of rewrites? Example of what I'm going for:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(page1|page2|page3)\.php\?parameter-view=([^\s]+) [NC]
RewriteRule ^ /(page1|page2|page3)/%1? [R=302,L]
RewriteRule ^(page1|page2|page3)/(.*)/(.*)/?$ /(page1|page2|page3).php?parameter-view=$1&parameter-id=$2 [L,QSA,NC]
RewriteRule ^(page1|page2|page3)/(.*)?$ /(page1|page2|page3).php?parameter-view=$1 [L,QSA,NC]
The best case scenario is i refine not only the idea of using the same rewrite for all pages but maybe not even having to use 3 to do the job?
Yes sure you can do something like this:
RewriteCond %{THE_REQUEST} \s/+(page1|page2|page3)\.php\?parameter-view=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
RewriteRule ^(page1|page2|page3)/(.*)/(.*)/?$ /$1.php?parameter-view=$2&parameter-id=$3 [L,QSA,NC]
RewriteRule ^(page1|page2|page3)/(.*)/?$ /$1.php?parameter-view=$2 [L,QSA,NC]

Resources