RewriteRule for Pretty URLs not working - .htaccess

The URLs of my site are of the type
http://localhost/cms2/pages.php?mpage=2
I have written the following .htaccess in order to create pretty URLs, but nothing happens
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)\/$ pages.php?spage=$1 [NC]
Mod-rewite is ennabled and other rewrite rules I've tested work. For example, I have a file called "contact-us.php". I can make it look "mysite.com/contact-us" using the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^(.*)\.html$ $1.php [nc]
Any help would be appreciated.
Thanks in advance.
Sonia

Not working because your regex is incorrect. Use this rule in your root .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)/([^./]+)/?$ /cms2/$2.php?spage=$1 [L,QSA]
This will rewrite a pretty URI: /3/page/ to /cms2/page.php?spage=3

Related

htaccess rule for pretty URLs

I have a web site working on localhost for now.
I send some data with get method to another controller
When I send data my url is look like:
http://localhost/book/Control/?kolon=&satir=unknown&modals=infox&bookid=4555
but i want to it look just like:
http://localhost/book/4555
I tried editing my .htaccess file like below but it does not work:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteRule ^(.*)$ Control/?kolon=&satir=unknown&modals=infox&bookid=$1 [L]
</IfModule>
how can I fix it?
You want the pretty URLs through htaccess.
The following is tested and works works:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^book/(\d+)*$ ./book/Control/?kolon=&satir=unknown&modals=infox&bookid=$1
Refer: https://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049
Try to change it in that way:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^book/(.*)$ Control/?kolon=&satir=unknown&modals=infox&bookid=$1 [L,QSA,NC]
If bookid it's a numeric value you can use:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^book/([0-9]+)$ Control/?kolon=&satir=unknown&modals=infox&bookid=$1 [L,QSA,NC]

Mod rewriting using .htaccess friendly url

My url is http://example.com/product/Braided/table-fan
And i want to rewrite like this http://example.com/Braided/table-fan
where product is my php file.
Current rules:
Options +MultiViews +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ product/$1/$2/$3
RewriteRule ^([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ product.php?uname=$1&pid$2
Try and use this in your root .htaccess:
RewriteEngine On
RewriteRule ^$ product/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ product/$1
This should remove product from your URL and leave you with: http://example.com/Braided/table-fan.
Make sure you clear your cache before testing this.

how to create friendly url in my directory using php

I use these type of URL:
http://test.com/rest/api.php?action=test
But I need these type URL:
http://test.com/rest/api/action/test
I try to .htaccess file these code here
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ api.php?rquest=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ api.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ api.php [QSA,NC,L]
Put this code by relacing your code your .htaccess under DOCUMENT_ROOT/rest directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /rest/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/([^/]+)/?$ api.php?action=$1 [QSA,NC,L]
This should fix it:
RewriteEngine On
RewriteRule ^rest/api.php?action=test$ /rest/api/action/test [L]
Your goal can be achieved by using regular expressions with syntax like :
RewriteRule ^rest/api/action/(\d+)*$ ./rest/api.php?action=$1
try read these articles :
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/ and
http://net.tutsplus.com/tutorials/other/the-ultimate-guide-to-htaccess-files/

htaccess for Dynamic URL Rewrite with .html extension based on url selection

I'm trying to rewrite some dynamic page url in which data is taken from database with slug like "this-is-a-link".
Options +FollowSymlinks
RewriteEngine On
RewriteBase /testweb/
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ faq_pages.php?page=$1 [QSA,L]
It works fine with http://www.test.com/testweb/this-is-a-link
But I need to change the rewrite rule based on any link clicked as below.
http://www.test.com/testweb/faq/this-is-a-link, 'testweb/news/this-is-another-link','/testweb/testimonials/test-link'
The "faq_pages.php" in the above RewriteRule should be changed to "news_pages.php". How can I have it dynamically so that I can use for every page with same htaccess or how can I check and rewrite the value to different pages in htaccess.
Also please advice me how to correct below code.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.html$ faq_pages.php?page=$1
I need to show html urls in the address bar like '/testweb/this-is-a-link.html' or '/testweb/faq/this-is-a-link.html'
Edit: By the help of Jon Lin I have updated my codes as below.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /testweb/
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^faq/(.*?)/?$ faq_pages.php?page=$1 [QSA]
Now both below conditions are working :
/testweb/faq/another-faq-test.html,
/testweb/faq/another-faq-test.html/
As I don't have any sub-folder with name 'faq' the template files(images/css/js etc) in root folder are not loading.
How can I fix it?
Thanking you
Are you looking for something like this?
Options +FollowSymlinks
RewriteEngine On
RewriteBase /testweb/
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^faq/(.*)$ faq_pages.php?page=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^news/(.*)$ news_pages.php?page=$1 [QSA,L]

.htaccess single variable url rewrite

I am trying to redirect http://test.pearlsquirrel.com/explore_all?u=hiphop to http://test.pearlsquirrel.com/explore_all/hiphop using htaccess. However, It keeps redirecting here: http://test.pearlsquirrel.com/explore_all/?u=site_error.php. I have looked all over Stackoverflow for the answer but I just can't seem to find it. I would really appreciate some help in fixing this problem, thanks!
Here is my .htaccess file
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.pearlsquirrel.com$ [NC]
RewriteRule ^(.*)$ http://pearlsquirrel.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+) explore_all?u=$1 [L]
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} [^=]+=([^=]+)
RewriteRule .* /%1? [L]
Will map this:
http://test.pearlsquirrel.com/explore_all?u=hiphop
To this:
http://test.pearlsquirrel.com/explore_all/hiphop
Replace the corresponding code in your .htaccess file with this one.
I have not checked the rules in your .htaccess file.
OPTION
Now, if it is the other way around, which I think it is, and the idea is to map this incoming URL:
http://test.pearlsquirrel.com/explore_all/hiphop
To this resource:
http://test.pearlsquirrel.com/explore_all?u=hiphop
Replace the above rule set with this one:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} .*/([^/]+)/?
RewriteRule .* http://test.pearlsquirrel.com/explore_all?u=%1 [L]

Resources