Having trouble with mod_rewrite - apache

I've been attempting to get a simple rewrite done on a script for over two weeks now.
While it seems like it would be a simple task, any attempts to rewrite..
http://thebluecorsair.com/rank/index.php?character=Timbervvoif
to: http://thebluecorsair.com/rank/Timbervvoif result in the typical 404 response.
I've tried about twenty different answers, pages, as well as rewriting directly with PHP with no luck.
Additionally, what would be the 301 redirect for this as well?
Thank you!

Generated via http://www.generateit.net/mod-rewrite :
RewriteEngine On
RewriteRule ^rank/([^/]*)$ /rank/index.php?character=$1 [L]

To resolve this, I used the following code.
RewriteRule ^([a-z]+)$ index.php?character=$1 [QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)?$ index.php?character=$1 [QSA,L]

Related

.htaccess rewrite for category with pagination

I'm trying to write a mod-rewrite rule to handle pagination links on my site.
I'd like my URL structure to be this https://example.com/category.php?name=category-link to https://example.com/category/category-link [without pagination]
and https://example.com/category.php?name=category-link&page=2 to https://example.com/category/category-link/2 [with pagination]
I've tried the following:
RewriteRule ^category/([0-9a-zA-Z-]+)/([0-9]+) category.php?name=$1&page=$2
https://example.com/category/category-link isn't working
https://example.com/category/category-link/1 is working
2. am i able to redirect localhost/article/4/ to localhost/article/4 [here 4 is an id]
Any guidance would be much appreciated.
Could you please try following, based on your shown samples only. Please make sure you clear your browser cache after placing these rules into your htaccess file.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/category/([0-9a-zA-Z-]+)/?$ [NC]
RewriteRule ^(.*)$ category.php?name=%1 [L]
RewriteRule ^category/([0-9a-zA-Z-]+)/([0-9]+)/? category.php?name=$1&page=$2 [L]
OR you could use it without RewriteCond too. Make sure you are putting either of these NOT both of them please.
RewriteEngine ON
RewriteRule ^category/([0-9a-zA-Z-]+)/?$ category.php?name=$1 [L]
RewriteRule ^category/([0-9a-zA-Z-]+)/([0-9]+)/? category.php?name=$1&page=$2 [L]

Rewrite Rule - need guidance

I need to do some redirecting to get some internal links to work but I'm having a complete block.
The url would be http://www.something.com/faqs/What_happens_if_I_move_home?
redirected to http://www.something.com/faqs/index/What_happens_if_I_move_home?
but it must look like the original url. I'm sure there is a simple answer but rewrite rules and regex are a mystery to me at times.
I did try RewriteRule ^faqs(/.*)?$ /faqs/index$1 [R,L,NC]
amongst many others!
try this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/faqs/index
RewriteRule ^faqs/(.*) /faqs/index/$1 [L,NC]

Redirect (almost) all requests to the top level url

I've looked at a lot of the other mod_rewrite questions here and tried most of them, but none seem to work for me. This is what I'd like to do.
Redirect all requests like http://abc.com/foobar to http://abc.com/
EXCEPT images and js, so requests like http://abc.com/images/foo/bar or http://abc.com/js/foo/bar
The URL bar should stay the same. So while http://abc.com/foobar loads http://abc.com/, the URL should read like the former
Ports should remain intact, so http://abc.com:8080/foobar should redirect to http://abc.com:8080
This is what I have in my .htaccess file
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(images/.*|js/.*)$
RewriteRule ^(.*)$ - [L]
The condition is working well, and images and js files are loading fine. I thought the last line would redirect everything else to just the base domain, but I'm still getting 404 errors when I test it out.
I don't want to use a rule like this
RewriteRule ^(.*)$ http://abc.com/ [L]
because the domain may be different in different deployments.
I think I just have a poor understanding of how this works, but I'm just missing something small. Can someone help me get this sorted out?
This is what I ended up using
RewriteEngine On
RewriteCond %{REQUEST_URI} !^(/*|/index.html)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^\.]+$ index.html [L]
It's not perfect, because the last rule only looks for files without a dot (.) in them. So it will not apply to http://abc.com/images/image.jpg but will apply to http://abc.com/images.
I don't really understand why I have to do it this way, but it works as it does.

How do I make .htaccess do what I want? :) (appending query string to url)

Currently my .htaccess looks like this...
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
It currently changes any /xxx.php file into /xxx. This is great for SEO. However, I also want Mr. htaccess to convert certain URLs into a URL + query string. For instance when user goes to
/specific/somerandominfo
Then somerandominfo is passed to the specific.php file. I normally have no problem doing this using rewrites, but because of my fancy catchall rewrite, I can't figure out how to do it.
For example if I add
RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC]
to my .htaccess, then hitting up /specific/somerandominfo just serves me a big fat 500 Internal Service Error.
Any help from you apache gurus out there would be so, so cool.
Thanks!
p.s. anybody want to also throw in any other cool SEO tricks that they like? I'll bake you cookies.
You are getting 500 error because your rules are creating an infinite cycle. Check apache error log to see if it is true. So you should design your rules properly. Maybe like that:
RewriteRule ^([^/]*)$ $1.php [L]
RewriteRule ^(.*)/(.*)$ $1.php?var=$2 [L]
RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC]
This is mostly correct. I'd just add the B flag, like this:
RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC,B]
This causes the capture group $1 to be properly escaped for use in query strings. Note that you can still use QSA to retain the query parameters used in the original request (in addition to somerandominfo).
Perhaps you'll want to post your actual RewriteRule.

mod_rewrite not using multiple rules

I'm still pretty lost with mod_rewrite as its incredibly new to me. I'm trying to set up a few rules for better urls. However, after playing around with it for awhile it appears that it only ever uses the first rule listed. For example, if i go to "/frontpage/some-post-slug" it works perfectly but if i go to "/page/some-page-slug" I get a 500 Internal Server Error. Does anyone have any idea what would be causing this?
my .htaccess file is in full below:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^frontpage/([A-Za-z0-9-]+)*$ /frontpage/?slug=$1 [NE,L]
RewriteRule ^page/([A-Za-z0-9-]+)*$ /page/?slug=$1 [NE,L]
</IfModule>
Thanks for any help!
What happens if you remove the * before the $-sign? I never use them and my rules are pretty much the same as yours.