mod_rewrite a double directory - regex

I'm using mod_rewrite in my .htaccess to change a double directory structure into a double GET query string like so:
URL: http://domain.com/test/me/
After mod_rewrite: http://domain.com/index.php?u=test&c=me
using the following code in my .htaccess file:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?u=$1&c=$2 [L]
That works great, but if a second directory is not specified (e.g. http://domain.com/test/) I want the c variable to equal "all" like so:
http://domain.com/index.php?u=test&c=all
How can I do this? Thanks, regex looks like klingon poetry to me. I've tried a few different variations of the above code with no success.
P.S. bonus points if you can add a trailing / even if one is not typed into the url box, so that http://domain.com/test/me is handled the same as http://domain.com/test/me/ and http://domain.com/test is treated the same as http://domain.com/test/

Like this?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)\/?$ index.php?u=$1&c=all
RewriteRule ^([^/]*)/([^/]+)\/?$ index.php?u=$1&c=$2 [L]
Also did the bonus :P

Related

How to replace the first parameter in a query string?

I'm trying to change this query "https://myurl.com/puff/?search_keyword=xyz" into this one "https://myurl.com/puff/?s=xyz".
I saw an answer similar to this, but I haven't used regular expressions before and had issues customizing it to what I need. I'm using WordPress and editing the .htaccess file to achieve this.
I looked into How to replace the first parameter name in query string using .htaccess? and tried to use that to customize my own .htaccess but had issues implementing it on my own.
This is what my current .htaccess file looks like:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /puffadvisor/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /puffadvisor/index.php [L]x
</IfModule>
# END WordPress
All help is greatly appreciated :) thank you so much.
You need a condition based on %QUERY_STRING and then references to the capture groups prefixed by %
RewriteEngine On
RewriteCond %{QUERY_STRING} (.*(?:^|&))seach_keyword((?:$|=|&).*)
RewriteRule /puff /puff?%1s%2
This {DOCUMENT_ROOT}/.htaccess replaces the parameter name search_keyword with s on /puff location, no matter at which position the parameter occurs.
You might want to rewrite all pathes
RewriteRule .* $0?%1s%2

.htaccess - rewrite multiple links (wildcard)

I have big set of links - example:
tempfile-1.php
tempfile-2.php
tempfile-3.php
...
tempfile-255.php
What I need to do is to redirect the files respectively to:
temp-file-1.php
temp-file-2.php
temp-file-3.php
...
temp-file-255.php
I know how to rewrite it one by one, but that is not as solution.
Can you help to rewrite these type of links in wildcard rule/cond?
Thanks a lot in Advance.
Assuming keyword file is present in all the links, you can use this single generic redirect rule in your site root .htaccess to redirect all links:
RewriteEngine On
RewriteRule ^(temp)(file-.+\.php)$ /$1-$2 [L,NC,R=301]
(temp.+) is 1st group that matches starting temp
file-\d+\.php is 2nd captured group that starts with file and ends with .php
Try it like this, when working correctly you can change R=302 to R=301.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)(file-\d+).php$ $1-file-$2.php [R=302,L]

htaccess redirect non existent path to a script

I've a problem with one htaccess rule, i need that if someone call an non existent url with a specific syntax like:
http://www.somesite.com/somefolder/somefolder/123456978/unexistant_folder_nor_file_2015
where http://www.somesite.com/somefolder/somefolder/ exist and
123456978/unexistant_folder_nor_file_2015 does not exist
the URL called will be passed to a script with below syntax:
http://www.somesite.com/somefolder/somefolder/index.php?id=/12345678/unexistant_folder_nor_file_2015
(where somefolder is always a folder with same name like, as example, test)
i tried below code:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^somefolder/([^/]+)/([^/]+)$ /somefolder/somefolder/index.php?id=$1 [L, NC]
but doesn't work can someone give me some advice to solve this problem?
thank you
You can use this rule inside /somefolder/somefolder/.htaccess:
RewriteEngine On
RewriteBase /somefolder/somefolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\d+/.*)$ index.php?id=$1 [L,QSA]

Apache RewriteCond: how to match only top-level requests (no subdirectory)

After banging my head against this for the better part of a week, it turned out to be the same problem, and solution, as in this thread: RewriteCond in .htaccess with negated regex condition doesn't work?
TL;DR: I had deleted my 404 document at some point. This was causing Apache to run through the rules again when it tried to serve the new page and couldn't. On the second trip through, it would always match my special conditions.
I'm having endless trouble with this regex, and I don't know whether it's because I'm missing something about RewriteCond or what.
Simply, I want to match only top-level requests, meaning any request with no subdirectory. For example I want to match site.com/index.html, but not site.com/subdirectory/index.html.
I thought I would be able to accomplish it with this:
RewriteCond %{REQUEST_URI} !/[^/]+/.*
The interesting thing is, it doesn't work but the reverse does. For example:
RewriteCond %{REQUEST_URI} /[^/]+/.*
That will detect when there is a subdirectory. And it will omit top-level requests (site.com/toplevelurl). But when I put the exclamation point in front to reverse the rule (which RewriteCond is supposed to allow), it stops matching anything.
I've tried many different flavors of regex and different patterns that should work, but none seem to. Any help would be appreciated. this Stack Overflow answer seems like it should answer it but does not work for me.
I've also tested it with this .htaccess rule tester, and my patterns work in the tester, they just don't work on the actual server.
Edit: by request, here is my .htaccess. It allows URLs without file extensions and also does something similar to a custom 404 page (although its purpose is to allow filenames as arguments, not be a 404 replacement).
Options +FollowSymLinks
DirectoryIndex index.php index.html index.htm
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} =/home/me/public_html/site/
RewriteRule ^(.*)$ index.php
RewriteCond %{REQUEST_FILENAME} !-f # Below this is where I would like the new rule
RewriteRule ^(.*)$ newurl.php
</IfModule>
I want to match site.com/index.html, but not site.com/subdirectory/index.html
You can use:
RewriteRule ^[^/]+/?$
Or using RewriteCond:
RewriteCond %{REQUEST_URI} ^/[^/]+/?$

Apache rewrite/redirect directory to querystring

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]