.htacces mod_rewrite optional parameter - .htaccess

To have nice URLs I edited my .htacces file
domain.com/category/content
should be redirected to
domain.com/index.php?category=cat&content=cont
this works fine with this line
RewriteRule ^([^/]*)/([^/]*)$ /index.php?category=$1&content=$2 [L]
but
domain.com/category
should be redirected to
domain.com/index.php?category=cat
to display the contents of the whole category
I'm sure this is easy to solve but it is very confusing to me.

Use 2 rules:
RewriteRule ^([^/]+)/([^/]+)$ /index.php?category=$1&content=$2 [L]
RewriteCond %{REQUEST_URI} !/index.php
RewriteRule ^([^/]+)/?$ /index.php?category=$1 [L]

Related

Rewriterule for PHP files fails

I want to rewrite these .htaccess rules into a templates to use it for my other pages as well:
RewriteRule ^admin/add-news/?$ admin/add_news.php [L]
RewriteRule ^admin/edit-news/([0-9a-z-#._]+)/([0-9]+)/?$ admin/edit_news.php?name=$1&id=$2 [L]
For example:
RewriteRule ^admin/([a-z-])/?$ admin/$1.php [L]
RewriteRule ^admin/([a-z-])/([0-9a-z-#._]+)/([0-9]+)/?$ admin/$1.php?name=$2&id=$3 [L]
I tested the latest rules on WAMP and it does not work. It returns: 404 Not Found error
The requested URL was not found on this server.
The full .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^admin/add-news/?$ admin/add_news.php [L]
RewriteRule ^admin/edit-news/([0-9a-z-#._]+)/([0-9]+)/?$ admin/edit_news.php?name=$1&id=$2 [L]
Files locations are:
1. C:\wamp64\www\test.com
2. C:\wamp64\www\test.com\admin\add_news.php
C:\wamp64\www\test.com\admin\edit_news.php
3. I want redirect from URLs with underscores to hyphens, for example:
from admin/add_news.php to admin/add-news
Also from admin/edit_news.php?name=%1&id=%2 to admin/edit-news/name/id
Example: admin/edit-news/my-news-title/2
Any ideas how to fix it? Thank you.
With your shown attempts, please try following htaccess rules file.
Make sure that your .htaccess is present along with your admin folder AND make sure to clear your browser cache before testing your URLs.
Samples specific rules:
RewriteEngine ON
RewriteBase /test.com/
##First rule for hitting url admin/add-news in browser.
RewriteRule ^test\.com/admin/([\w-]+)/?$ admin/$1.php [QSA,NC,L]
##Second rule for hitting url admin/edit-news/name/id in browser.
RewriteRule ^test\.com/admin/([\w-]+)/([^/]*)/(\d)+/?$ admin/$1.php?name=$2&id=$3 [QSA,NC,L]
Generic rules:
RewriteEngine ON
RewriteBase /test.com/
##First rule for hitting url admin/add-news in browser.
RewriteRule ^test\.com/admin/([^-]*)-([^/]*)/?$ admin/$1_$2.php [QSA,NC,L]
##Second rule for hitting url admin/edit-news/name/id in browser.
RewriteRule ^test\.com/admin/([^-]*)-([^/]*)/([^/]*)/(\d)+/?$ admin/$1_$2.php?name=$3&id=$4 [QSA,NC,L]

htaccess rewriterule language redirect

I want to make my new website project multilingual. My idea is to redirect files to an imaginary language folder(like www.domain.com/en/sub/content.php and so on).
RewriteRule ^en/(.*)$ /$1?lang=en [L]
RewriteRule ^de/(.*)$ /$1?lang=de [L]
RewriteRule ^es/(.*)$ /$1?lang=es [L]
RewriteRule ^ru/(.*)$ /$1?lang=ru [L]
It works well for physically existing test files, even down a few folders...
But it doesn't work combined with another rewrite.
To get "www.domain.com/liga.php?whatleague=firstleague&matchday=7" to look like "www.domain.com/firstleague/2013/7/"
I used:
RewriteRule ^firstleague/2013/([0-9]+)/?$ /liga.php?whatleague=firstleague&matchday=$1 [L]
The redirect alone works fine, BUT the "lang" parameter doesn't get passed on when I add the language folder (www.domain.com/de/firstleague/2013/7/).I tried it in different order in the htaccess file, but with the same result.
PS: english is not my primary language.
You don't need another rule, just add QSA flag in you firstleague folder:
RewriteRule ^en/(.*)$ /$1?lang=en [L,QSA]
RewriteRule ^de/(.*)$ /$1?lang=de [L,QSA]
RewriteRule ^es/(.*)$ /$1?lang=es [L,QSA]
RewriteRule ^ru/(.*)$ /$1?lang=ru [L,QSA]
RewriteRule ^(firstleague)/2013/([0-9]+)/?$ /liga.php?whatleague=$1&matchday=$2 [L,QSA]

htaccess similar file names goes to same page

Most of the RewriteRules are working fine but there are a couple that have the same word in them and aren't going to the correct page.
Here is my full htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
# Rewrites for main pages
RewriteRule home index.php
RewriteRule about-us about-us.php
RewriteRule business-advice business-advice.php
RewriteRule associates associates.php
RewriteRule become-an-associate associates-sign-up.php
RewriteRule blog blog.php
RewriteRule contact-us contact-us.php
RewriteRule log-in log-in.php
RewriteRule sign-up sign-up.php
The problem resides within the two associated links. When I go to [MYURL]/associates it works fine but if I go to [MYURL]/become-an-associate it takes me to the correct URL but shows content from [MYURL]/associates
Anybody got any ideas?
Thanks,
The pattern for RewriteRule's are regular expressions and the rules all loop until the URI stops changing. That means the first time around, when you request /become-an-associate, it matches and is rewritten to /associates-sign-up.php. Then, the second time around, the rule with the pattern associates matches because of "/associates-sign-up.php". You need to add boundary checks (e.g. ^ and $) as well as the [L] flag:
# Rewrites for main pages
RewriteRule ^home/?$ index.php [L]
RewriteRule ^about-us/?$ about-us.php [L]
RewriteRule ^business-advice/?$ business-advice.php [L]
RewriteRule ^associates/?$ associates.php [L]
RewriteRule ^become-an-associate/?$ associates-sign-up.php [L]
RewriteRule ^blog/?$ blog.php [L]
RewriteRule ^contact-us/?$ contact-us.php [L]
RewriteRule ^log-in/?$ log-in.php [L]
RewriteRule ^sign-up/?$ sign-up.php [L]

trying to rewrite url in htaccess

I have url
http://www.url.com/business/index.php?biz=andrewliu
I'm trying to accomplish so it will be
http://www.url.com/business/andrewliu
I tried to have this:
RewriteRule ^/?([a-z0-9_-]+)/?$ /index.php?biz=$1 [L]
or
RewriteRule ^/?([a-z0-9_-]+)/?$ /business/index.php?biz=$1 [L]
doesn't work?
Help me?
Edit:
I have this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
prior to the rewriterule
That depends in which directory your .htaccess file is.
For the root directory try this one:
RewriteRule ^/?business/([a-zA-Z0-9_-\.]+)/?$ /business/index.php?biz=$1 [L]
If your .htaccess file is in the business directory that of your statments should work fine:
RewriteRule ^/?([a-zA-Z0-9_-\.]+)/?$ /index.php?biz=$1 [L]
RewriteRule ^business/([_0-9a-zA-Z-]+)/?$ business/index.php?b=$1 [L]
It's not clear from your examples whether you want "biz" or "business" or "b". I'm taking a guess that you want "biz", in which case your rule should be:
RewriteRule business/(.*) /index.php?biz=$1 [L]
Or maybe you want:
RewriteRule ([^\/]*)/(.*) /index.php?$1=$2 [L]

Remove subdirectory & remove .php extention in htaccess not working?

I m new to url rewrite:
First, here is what I am trying to accomplish:
Current URL: www.example.com/subdirectory1/subdirectory2/something.php
Desired URL: www.example.com/subdirectory1/something/
And, the name of subdirectory2 is fixed.
Possible?
My current htaccess just to remove the ".php" but also not working. (Any idea how to debug htaccess??)
RewritEngine on
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond %{REQUEST_URI} [^/]$
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^(?!subdirectory1/|subdirectory2/)(.+)$ subdirectory1/$1 [L]
Thanks.
Your first problem is RewritEngine on. You are missing an e. Should be RewriteEngine on.
Try this:
RewriteEngine on
# Remove .php
RewriteCond %{REQUEST_URI} \.php$
RewriteRule ^([^/]+)/fixed/([^/]+).php$ /$1/$2/ [R=301,L]
# Rewrite "friendly" URL into php.
RewriteRule ^([^/]+)/([^/]+)/?$ /$1/fixed/$2.php [L]
This only works for exactly what you said. Fixed is always the same. Replace it with the correct value.
The users goes to: www.example.com/1234/fixed/5678.php. He is redirected to www.example.com/1234/5678
User goes to www.example.com/1234/5678. On the server, this becomes www.example.com/1234/fixed/5678.php.
Something like www.example.com/1234/5678/9abcd will not work. (More than two levels of directories.)

Resources