Rewrite rules overriding - .htaccess

I have two rewrite rules which, in what ever order I add them, fail to work together
The first one takes a name from url e.g.
http://myUrl.com/JohnSmith
and forwards it to:
http://myUrl.com/pages/gf_profile.html?user=JohnSmith
Code is:
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ /pages/gf_profile.html?user=$1 [NC,L]
On it's own it works fine. The second one is wordpress, I have urls e.g.:
http://myUrl.com/blog/my-blog-post
And it uses:
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
With the second section in, when I try the first URL it thinks it's a blog title and therefore cannot find it.
Is there a way to use them both?

Try:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^([^\.]+)$ /pages/gf_profile.html?user=$1 [NC,L]
And I assume your other rules are in the /blog/ directory (otherwise it doesn't look like it'll work right).

Answer:
Thanks to #anubhava - I simply created a .htaccess file in the /blog/ folder with the following on and all works perfectly now.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
I'm not sure why wordpress doesn't add this on install, it should IMHO - hopefully this might help someone who has a similar issue down the line.

Related

Drop the .html from URL in .htaccess

I am having a hard time trying to drop the .html for our newly updated website through .htaccess. I have tried a number of different lines of code copy and pasted from the internet, but I'm sure it's something simple that I just don't have the experience to see!
In short, I want to make this request (which is a 404) drop the .html:
http://mysite/cmt-grooving-sblade.html
To this:
http://mysite/cmt-grooving-sblade
Any help you could give will be I will be very grateful for!
So far, my .htaccess looks like this. The first section was already there, the rest is my recent attempt.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
You can use this to remove .html from your URLs:
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
It will leave you with your desired URL: http://mysite/cmt-grooving-sblade. Make sure you clear your cache before testing this.
Try this;
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

htaccess rewrite rule for multiple pages

this seems so simple, but it's not working for me.
I am trying to have both show.php?id=$1 and producer.php?id=$1 rewrite as friendly URLs.
The first one works perfectly, the second does not. If I remove the first, the second works fine.
What am I doing wrong here?
My code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)\.html$ /show.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)\.html$ /producer.php?id=$1 [L]
</IfModule>
Thanks in advance!
Your problem is quite simple.
You cannot have more than one rewrite rule for the same directory, you would need to have a separate folder for each IE. have a folder called "show" and have the htaccess file that contains the rewrite data for show.
.htaccess for show folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)\.html$ /show.php?id=$1 [L]
</IfModule>
Then have another folder called "producer" with the 2nd rewrite rule.
.htaccess for producer folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)\.html$ /producer.php?id=$1 [L]
</IfModule>

Conditional rewriting in htaccess file

I'm rather new to the whole .htaccess thing and I'm using the following right now to use 'pretty url's':
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?path=$1 [NS,L]
</IfModule>
Now i found my website a bit slow and decided to start gzipping my CSS files thru a php script I found somewhere on the web (the website). For this to work I need to rewrite the url to open the correct php file. That would look something like this:
RewriteRule ^(.*).css$ /csszip.php?file=$1.css [L]
But I only want the first to happen when the second doesn't and vice versa. In other words i'd like something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
if request doesn't contain .css do
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?path=$1 [NS,L]
else do
RewriteRule ^(.*).css$ /csszip.php?file=$1.css [L]
</IfModule>
Can anyone help me with the proper code or a place where i can find a way to use some kind of conditional statement in htaccess files?
Thanks in Advance!:)
Try this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !.css
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?path=$1 [NS,L]
RewriteRule ^(.*).css$ /csszip.php?file=$1.css [L]
</IfModule>
Also, you can put the last rule on top

Remove ? with htaccess

I have a Wordpress installation that works and a htaccess-file:
# 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
Outside Wordpress I have created links like this:
http://www.test.com/butik/?my-url
I think it looks nice except for the "?". Is it simple to add something to my htaccess-file to remove it?
http://www.test.com/butik/my-url/ would be perfect. Does it matter if I run a file at the path http://www.test.com/butik/index.php? Would it collide?
If I'm understanding correctly, you can try the following:
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule (.*) $1%1? [L]
So, given your example, $1 would be /butik/ and %1 would be my-url (captured from the RewriteCond) Also, the first RewriteCond should prevent any conflicts with index.php requests.
Hope this helps.

Many RewriteBase in one .htaccess file?

I have a domain and a wordpress-blog on same server. Now I have a problem (surprise). The wordpress is located on /httpdocs/blog/ and domain is pointing to /httpdocs/ and I'm trying to redirect it to /httpdocs/domain/. But, obvisiously, I have permalinks in Wordpress.
Here's my current .htaccess:
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
RewriteBase /
RewriteCond %{HTTP_HOST} domain.com
RewriteCond %{REQUEST_URI} !^/domain
RewriteCond %{REQUEST_URI} !^/cgi-bin
RewriteRule ^(.*)$ domain/$1 [L]
But as you already propably assumed, this doesn't work. Wordpress' permalinks affects to /domain/ also, so my images and other urls go wrong.
Any advice? Is it possible to use RewriteBase like this?
No, you can only have one base URL. Just rewrite your rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/. /blog/index.php [L]
RewriteCond %{HTTP_HOST} =example.com
RewriteCond %{REQUEST_URI} !^/domain
RewriteCond %{REQUEST_URI} !^/cgi-bin
RewriteRule ^(.*)$ domain/$1 [L]
I come to this post when I am trying to find solution for a similar problem. It seems that there can be more then one base URL, but the logic does not stop after rewrite. If the URL hit both rewrite base, all the rewrite will be run. Therefore, the strictest rewrite base should be put at the end of the file. For this example, it should be:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} domain.com
RewriteCond %{REQUEST_URI} !^/domain
RewriteCond %{REQUEST_URI} !^/cgi-bin
RewriteRule ^(.*)$ domain/$1 [L]
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
Noticed that as both rewrite are being run, so if the rewrite contradicts, you will need to fall back to the accepted answer.

Resources