Apache rewrite/redirect directory to querystring - regex

I plan on learning the ins and outs of mod_rewrite, but I have a problem I would like solved before I get around to doing that, and I'm probably only missing something small here anyway.
I want to force redirect any directory that contains digits [0-9]{1,4} to a single php file with a querystring of that number.
For example
http://example.com/23
or
http://example.com/23/
would redirect to:
http://example.com?23
(the .php file is my index)
Currently I have the following:
RewriteCond $/([0-9]{1,4})/^
RewriteRule $/([0-9]+)^ ?%1 [R]
Which throws a 500...
Thanks for your time.

Try with
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]{1,4})$ ?$1 [QSA,L]

Related

Url rewriting using RewriteCond whrere different url required based on query param

My requirement is
domain.com/src/home.php?page=category&category=recipe
domain.com/src/home.php?page=article&key=myarticle
domain.com/src/home.php
Expected end url
domain.com/src/category/recipe
domain.com/src/article/myarticle
domain.com
I have written one
RewriteRule ^src/([A-Za-z0-9-]+)/?$ src/home.php?page=article&key=$1 [NC,L]
The problem is if one is working then the other is not.
Can anyone please suggest how to make it work with some condition based on "page" query param.
Thanks in advance.
With your shown samples, attempts, please try following htaccess Rules file. Make sure to keep your htaccess file along with src folder(not inside it).
Also please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /src/
##Rules for handling url domain.com/src/category/recipe here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^src/(category)/([\w-]+)/?$ src/home.php?page=$1&category=$2 [QSA,NC,L]
##Rules for handling urls like: domain.com/src/article/myarticle
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^src/(articile)/([\w-]+)/?$ src/home.php?page=$1&key=$2 [QSA,NC,L]
##Rule for handling domain.com url here.
RewriteRule ^/?$ src/home.php [QSA,L]

RewriteRule with two get parameters

I have a folder/file structure like this:
www.example.com/a/<optional subfolder>/file.png
and I want to have a RewriteRule that passes the "a" as the first parameter and all the stuff after that (file.png or subfolders/file.png) to a file called file.php, so if a user visits
www.example.com/a/file.png (this is not an actual file path, it's somewhere else)
it should be rewritten to
www.example.com/file.php?user=a&file=file.png
and
www.example.com/a/random/subfolders/here/file.png
will be rewritten to
www.example.com/file.php?user=a&file=random/subfolders/here/file.png
I tried a whole lot of RewriteRules I found on the Internet, one of them even worked but not for subfolders. As I do not know one thing about RegEx, I'd really appreciate your help ^^
Try adding this rule to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.+)$ /file.php?user=$1&file=$2 [L,QSA]

URL Rewriting and htaccess files

Hoping someone can help. I use nibbler to check out my website to make sure it behaves well and one of the categories is URL FORMAT which
Avoid use of file extensions wherever possible.... Consider URL rewriting as an effective
and transparent means of creating appropriate URLs.
Anyway, I use Dreamweaver to create and edit the site and if I convert it to using index.htm files in directories (which would solve this) then this just seems to make life complicated.
Is there some MAGIC that I can do in the .htaccess file that means i can still upload it as .htm's and it does some cool stuff in the background and shows them better.
for example I would like
http://www.beingchildren.org/Children-Charity-Blog.htm
to become
http://www.beingchildren.org/Children-Charity-Blog
Can anyone help?
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.htm[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.htm -f
RewriteRule ^(.+?)/?$ /$1.htm [L]

URL rewriting with Apache .htaccess with two matches

Sorry about this question - I know this is asked a lot, but this case is a little more distinct (for me at least).
I have the following content in my current .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?mode=$1 [QSA,L]
This rewrites all URLs that are from index.php and have the mode parameter to a simply URL where the mode's content becomes the main part of the URL, like this:
http://someurl.com/index.php?mode=mymode
Becomes:
http://someurl.com/mymode
This is exactly what I need. But I also need to extend this and be able to achieve the same effect with another file of mine named user.php. The case is almost the same:
http://someurl.com/user.php?action=hello
Becomes:
http://someurl.com/hello
I have no idea how to achieve the second part without conflincting with the first one.
I'm kinda stuck on this one.
Keep your rules like this:
# new rules for /user.php?action=hello
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/([^/]+)/?$ user.php?action=$1 [QSA,L]
# existing rule for /index.php?mode=mymode
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?mode=$1 [QSA,L]
PS: Also better to use absolute path in your css, js, images files rather than arelative one. Which means you have to make sure path of these files start either with http:// or a slash /.

.htaccess issue, existing .php files and search.php

I'm new to playing with .htaccess for nicely formatted urls and I'm just not sure I'm doing it right.
My current .htaccess file looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/(.*) search.php?query=$1 [L]
RewriteRule !\.(gif|jpg|ico|css|js|txt|xml|png|swf)$ index.php
What I want is for mysite.com/home/56/page-title to go to index.php where I filter out the number and load the correct index page, which works fine.
Before that, I want to check if the url is pointing at search.php, and if so redirect to mysite.com/search/the-search-term, this also works fine.
What isn't working is if I try to visit a specific .php file say mysite.com/control_panel.php - it just takes me to index.php, but I thought that the following line stopped that from happening?
RewriteCond %{REQUEST_FILENAME} !-f
If someone could explain it to me that would be great :)
Thanks!
1. Order of rules matters
2. RewriteCond directives will only be applied to the ONE RewriteRule that follows it. If you need to apply the same conditions to multiple rules you have to write them multiple times or change the rewrite processing logic (multiple approaches available).
Try this one:
RewriteEngine On
RewriteRule ^search/(.*) search.php?query=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(gif|jpg|ico|css|js|txt|xml|png|swf)$ index.php [L]