mod_rewrite not Working in grabkart.com [duplicate] - .htaccess

This question already has an answer here:
How to convert querystring to paths URL using mod_rewrite?
(1 answer)
Closed 8 years ago.
I Have My Own Website grabkart
Grabkart.com is online shopping portle
I want To make Change url of my website from
http://www.grabkart.com/productdetail.php?prodid=4385
to
http://www.grabkart.com/productdetail/4385
What Change I Have to make in my.htaccess file for this

RewriteEngine on
RewriteRule ^productdetail/([0-9]+)/?$ productdetail.php?prodid=$1

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Place the above in your .htacces, and assuming you have full admin rights on your server, and can access the hidden files - this should work.
In the above code the -d stands for Directory and -f for Regular File.
and the above will remove the .php extension from your URLs.
For example, if your page name is test.php in the main directory then you may access your page with the following urls:
grabkart/test
or
grabkart/test.php.
This will works in both conditions, and the same logic can be applied to anything you would like removing from the path.

Related

htaccess: rewrite URL to a file only if that file exists, but with regex captured groups

I have a CMS that I've built myself in PHP (Codeigniter framework). I was thinking why every time PHP has to process all code just that to respond with a page. So instead I will create the complete HTML pages and serve them when a user asks for them.
That is why I need to rewrite an URL to a specific file only if that file exists. For this, I need to use regex captured groups because I want to build this for all files in that folder and subfolders.
For example, I want to put a bunch of HTML pages on %{DOCUMENT_ROOT}/out and want to access them directly with rewrite rules.
For example, if the URL is like this:
htaaatps://www.dsaoidashd.com/services/development-of-desktop-applications
I want to look at the following location:
%{DOCUMENT_ROOT}/out/services
for
development-of-desktop-applications.html
and if it exists, I want to return it to the client browser without continuing.
And of course, if I have yet more levels like this:
htaaatps://www.dsaoidashd.com/services/development/of-desktop-applications
then I need to check this location:
%{DOCUMENT_ROOT}/out/services/development
for
of-desktop-applications.html
file and return it to the calling browser.
I tried
I know that I have to use RewriteCond to check if the file exists but how to pass it to the RewriteCond regex captured groups so I can check them based on the URL provided?
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/out/$1.html -f
RewriteRule ^.*$ /$1.html [L]
Your RewriteCond is almost correct but you have to capture $1 in a group in RewriteRule and also your target needs to be out/$1.html.
You can use this rewrite rule in your site root .htaccess:
RewriteEngine On
# To internally forward /dir/file to /out/dir/file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/out/$1.html -f
RewriteRule ^(.+?)/?$ out/$1.html [L]

Make friendly URL in php with .htaccess [duplicate]

This question already has answers here:
How to create friendly URL in php?
(8 answers)
Closed 1 year ago.
I am trying to implement SEO friendly URLs using .htaccess and PHP.
My URL is:
http://www.my_web_site.com/instructions.php?topic_id=6&category=NNNX&t=a-b-c
I want to this:
http://www.my_web_site/6/a-b-c
Can someone please help me make a following .htaccess content
Thanks.
You can use the following rule in your htaccess file to shorten your URLs
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/(.+)$ /instructions.php?topic_id=$1&category=NNNX&t=$2 [L]
With this rule , you can just type example.com/6/foobar instead of the full old URL path.

Htaccess for redirecting a directory to a file with the same name

After doing a quick lookup on how to manage sites with multiple language support, I find site.com/language/page/ the neatest url layout. (as I don't have the funds for site.language)
I have used htaccess to redirect the base site from site.com to site.com/language/
by using: RedirectMatch ^/$ /language/ where 'language' is language.html
But since I have a directory called /language/ so that all other pages in the given language can be put inside it, the site just shows up as the index of the directory.
How can I accomplish that kind of layout, if I want the main index page to show up in the url as site.com/language/ ?
Current htaccess that worked fine until I added the directory:
## Rewrite Defaults
RewriteEngine On
## Remove file extension + force trailing slash
RewriteCond %{REQUEST_URI} (.*)/$
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule (.*)/$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule .* %{REQUEST_FILENAME}/ [R=301,L]
## Redirect to /en-gb
RedirectMatch ^/$ /en-gb/
With the root folder looking like:
/language
stuff.html
morestuff.html
language.html
...
I would really appreciate help as this is currently a nightmare situation. When I try compiling the htaccess with answers to similar questions, everyone has just parts of what I am looking to achieve and thus I break the layout with every modification...
Is it possible to change the back-end filenames and accomplish this layout using only htaccess for front-end url rewriting since the url bar is the only thing that matters?
If I understand correctly, you should be able to solve this by removing the line:
RewriteCond %{REQUEST_FILENAME} !-d
Let me know if that works.

WordPress like .htacces nice urls [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
.htacces to create friendly URLs. Help needed
I want to build a script like WordPress for the "nice urls", i copied the .htaccess rows and made some modifications, but on the side of the PHP i don't know how to build it.
I don't understand how it works.
With the help of Daniel A White i found the class in WordPress files.
WP_Rewrite: http://xref.wordpress.org/trunk/WordPress/Rewrite/WP_Rewrite.html
Start with setting rewrite engine and follow sym links
RewriteEngine On
# Drop the .php -- or any other extension you use.
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#Forces the site to switch to www.site.com - change the site/TDL below
RewriteCond %{HTTP_HOST} ^insuranceiwant\.com$
RewriteRule (.*) http://www.insuranceiwant.com/$1 [R=301,L]
Now name your pages something useful like site.com/contact-us
If you are pulling dynamic pages from a database, also add this:
Options +FollowSymLinks
Options +Indexes
RewriteRule ^([A-Za-z_]+)_([A-Z]+)/([A-Za-z0-9_-]+)$ /Resources/city.php?state=$1&region=$2&city=$3
That example shows that the dynamic page has a state name, state abbreviation, and a city pulling from the database and showing the page like so:
insuranceiwant.com/California_CA/Los_Angeles
For more information on regular expresion for your page names check out this useful site:
http://www.regular-expressions.info/

How to setup optional parameters in mod_rewrite

I am in a new project and I'm designing the URL structure,
the thing is I want URLs look like this:
/category-23/keyword/5/
Where the normal page is:
/search.php?q=keyword&cat=23&page=5
So my question is, cat and page fields, must be optional, I mean if I go to /keyword it should be
/search.php?q=keyword (page 1)
and if I go to
/category/keyword should be:
/search.php?q=keyword&cat=category&p=1
and also if I go to
/keyword/5/ it must be: /search.php?q=keyword&p=5
Now I have my .htaccess like this:
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)$ search.php?q=$2&cat=$1&page=$3 [L]
I cannot make it working and the CSS / image files don't load.
I'd thank a lot who could give me a solution.
You can do this with four rules, one for each case:
RewriteRule ^([^/]+)$ search.php?q=$1
RewriteRule ^([^/]+)/([0-9]+)$ search.php?q=$2&p=$1
RewriteRule ^([^/]+)/([^/]+)$ search.php?q=$2&cat=$1&p=1
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)$ search.php?q=$2&cat=$1&p=$3
And with this rule in front of the other rules, any request that can be mapped onto existing files will be passed through:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^ - [L]
Now your last issue, that externally linked resources cannot be found, is due to that you’re probably using relative URL paths like css/style.css or ./css/style.css. These relative references are resolved from the base URL path that is the URL path of the URL of the document the references are used in. So in case /category/keyword is requested, a relative reference like css/style.css is resolved to /category/keyword/css/style.css and not /css/style.css. Using the absolute URL path /css/style.css makes it independent from the actual base URL path
While i know this was answered succinctly by #Gumbo a few months back, I ran into a similar issue recently... and didn't want to include full/absolute paths in my app, to keep it dynamic and not having a bunch APP_PATH (php) vars all over the place... so I just added a base[href] html tag with the
like so...
<base href="http://<?php echo $_SERVER[HTTP_HOST];?><?php echo APP_PATH;?>"/>
Hoping that helps others... and this is not trying to discount #Gumbo's reply in the least... they're right-e-o on :).
Shouldn't it be
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^ - [L]
in #Gumbo's answer above? Works with me like that. If it is a file (like CSS), let it pass through.

Resources