beginners guide for php htaccess - php

I have just started learning htaccess.
And i want to test one page :
I have one folder called "favs" and inside that i have a php file index and from that i am passing to the page gallery.php with querystring ID
so the url will be :
mainsite.com/favs/gallery.php?id=7
Now i want the url to be like :
mainsite.com/favs/7
and this is the code i write in .htaccess file :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ gallery.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ gallery.php?id=$1
</IfModule>

Your rules are wrong. You have this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ gallery.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ gallery.php?id=$1
</IfModule>
^([a-zA-Z0-9_-]+)$ will match everything in the URL after the first slash, as long as there is no slash. There's a slash in mainsite.com/favs/7, so the rule doesn't match, and it looks for an actual directory named /favs/7. What you want is to match only the piece after favs, like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^favs/([0-9]+)/?$ favs/gallery.php?id=$1
</IfModule>
The code above is what you should put in .htaccess if it is in your root directory. If it is in your /favs directory, then you need to remove the /favs from RewriteRule and add a RewriteBase like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /favs
RewriteRule ^([0-9]+)/?$ gallery.php?id=$1
</IfModule>
Note: based on your question, I have eliminated letters, _, and - from the regular expression. If you need to match them, you can tweak the rule above accordingly.

the is my full code :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteEngine On
RewriteRule ^favs/([0-9]+)/?$ favs/gallery.php?id=$1
</IfModule>
and this is the url :
http://www.fullridescholarships.net/favs/

Related

htaccess file has no affect

I'm trying to rewrite an url like this:
www.mywebsite.com/product/a-product-name-here/92341
to
www.mywebsite.com/our-products/a-product-name-here/92341
I'm using following rewriterule:
RewriteRule ^product/(.*)$ our-products/$ [R=301,L]
And according to this online tool it should do the trick. I've put this online, but it didn't affect anything.
AllowOverride is active on my host.
My folder structure is like this (tried the rewrite rule in both of the 2 .htaccess's):
UPDATE
This is the .htaccess content in the folder Corporate
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /corporate/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /corporate/index.php [L]
</IfModule>
# END WordPress
And this is the content of the .htaccess file in the product folder:
AddDefaultCharset utf-8
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.+) index.php?a=$1 [nc]
Alright, as far as I understand, your problem can be divided into two parts.
Redirect public URL
You want the public URL example.com/product/foo-bar/1234 to be redirected to example.com/our-products/foo-bar/1234. This can be achieved with the already posted rule by anubhava, slightly modified to only match numbers:
Put it in your root .htaccess, besides index.php.
RewriteRule ^product/((.+)/(\d+))$ /our-products/$1 [R=301,L]
Map external URL to script, internally
Then, the external URL is example.com/our-products/foo-bar/1234, which does not physically exist on the server machine and therefore fails with a 404.
So you'll have to map it to the script containing the actual logic.
RewriteRule ^our-products/(.+)$ /product/index.php?a=$1 [L]
This is assuming that your script will accept the a parameter.
Your .htaccess files, after the changes
This is the .htaccess content in the folder Corporate
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^product/((.+)/(\d+))$ /our-products/$1 [R=301,L]
RewriteRule ^our-products/(.+)$ /product/index.php?a=$1 [L]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /corporate/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /corporate/index.php [L]
</IfModule>
# END WordPress
And this is the content of the .htaccess file in the product folder:
AddDefaultCharset utf-8
Options +FollowSymlinks

Using .htaccess to rewrite urls with ID: "Page not found"

I am trying to rewrite some urls by using the .htaccess file in a WordPress environment. I have a WordPress page called "offerta" with two parameters, one of them is a custom ID that I use to generate content.
so, an example url is: http://www.riccionelastminute.it/rlm/offerta/?id=15&titolo=my-title
and I would like it to be: http://www.riccionelastminute.it/rlm/offerta/my-title-15/
this is my .htaccess right now:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /rlm/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /rlm/index.php [L]
</IfModule>
# END WordPress
After some research, I tried to add this rule right after the last Rewrite Rule, but it redirects to a 404 page:
RewriteRule ^offerta/([A-Za-z0-9-]+)-([0-9]+)/$ offerta/?id=$2&titolo=$1
I'm not used to play around .htaccess and I'm not an expert.
Am I missing something?
The problem is that the last RewriteRule has the L flag which means that no other rules are applied after it. What you need to do is add your rule before the last one:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /rlm/
RewriteRule ^index\.php$ - [L]
RewriteRule ^offerta/([A-Za-z0-9-]+)-([0-9]+)/$ offerta/?id=$2&titolo=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /rlm/index.php [L]
</IfModule>

How do i shorten my url like facebook do to users profile page

I am trying to shorten my website url of a newly added page event like
mysite.com/asoebi/myevent instead of mysite.com/asoebi/index.php?event=myevent
but every time i try to change my .htaccess file it give an error
here is .htaccess code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^index/(\w+)$ ./index.php?event=$1
</IfModule>
You have to enable mod_rewrite in apache for this to work.
Put this one in the .htaccess file in the /public_html/asoebi/ folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /asoebi/
RewriteRule ^(.*)$ index.php?event=$1 [L]
</IfModule>
Also remove the last rewriterule from the main htaccess file you posted above

How to rewrite the url by htaccess and also replace the underscore with dash

I have a WordPress site running in an apache server, I want to rewrite some the URL in SEO friendly manner, so how to do that
Currently, I have this URL
www.example.com/about-us/?id=what_we_do_1
www.example.com/about-us/?id=why_us
I want it like
www.example.com/about-us/what-we-do-1
www.example.com/about-us/why-us
and for this
www.example.com/media/?id=team
www.example.com/media/?id=abc_xyz
I want something like this
www.example.com/media/team
www.example.com/media/abc-xyz
my current .htaccess is like the below
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
To convert urls like
/index.php?p=test
to
/index.php/test
RewriteEngine On
RewriteCond %{QUERY_STRING} p=(.+)
RewriteRule ^/?index.php$ /index.php/%1? [R,L]
Thus in your case
RewriteEngine On
RewriteCond %{QUERY_STRING} p=(.+)
RewriteRule ^/?about-us?$ /about-us/%1? [R,L]
Similarly for other pages
You can try these to convert underscore to dash :-
RewriteEngine On
RewriteBase /
After that for another url :-
1) www.example.com/about-us/?id=what_we_do_1
htaccess rule will be :-
RewriteRule ^about-us/(.*)$ about-us/?id=$1 [QSA,L]
It may help you.

How to change Our website URL using htaccess

I'm new in PHP so I faced some problem using URL redirect using .htacess.
My URL page look like this: http://domain.com/blog_detail.php?id=blog_title
But I want change URL using .htaceess like this: http://domain.com/blog/blog_title
I have tried this, but it's not working:
<IfModule mod_rewrite.c>
RewriteEngine on # Turn on the rewriting engine
RewriteRule ^blog/([a-zA-Z0-9_-]+)$ blog_detail.php?id=$1
</IfModule>
I would use:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f ## respect real files
RewriteCond %{REQUEST_FILENAME} !-d ## respect real directories
RewriteBase /
RewriteRule ^blog/(.*)$ blog_detail.php?id=$1&%{QUERY_STRING} [L]
&%{QUERY_STRING} only if you want to pass other variables here and there like:
http://domain.com/blog/blog_title?lang=fr
for instance
Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^(.*)$ blog_detail.php?id=$1 [QSA,L]
</IfModule>

Categories