.htaccess RewriteRule with parameters doesn't seem to be working - .htaccess

I managed to get this Rewriterule to work :
RewriteRule ^([a-z-]+)\/?$ $1.php [NC]
that basically rewrites my_project/admin.php -> my_project/admin & my_project/login-admin.php -> my_project/login-admin
The problem is that my second rule is for my page that adds users into my database using a table parameter isn't working, what i need to show on the URL is my file name "add-client" and the table parameter "tb1", it should look like this
my_project/add-client.php?tb=tb1 -> my_project/add-client/tb1
i tried this, but it doesn't work:
RewriteRule ^([a-z-]+)\/?$ $1.php?tb=$2 [NC]
Does the fact that those two rules are in the same htaccess file makes any problem? please help, thank you in advance.

Here is how you can set htacces.
my_project/admin.php -> my_project/admin AND my_project/login-admin.php -> my_project/login-admin
RewriteRule ^my_project/admin$ my_project/admin.php [L,NC]
my_project/add-client.php?table=table1 -> my_project/add-client/table1
RewriteRule ^my_project/([a-zA-Z0-9_-]+)$ yourfile.php?tb=$1 [L,NC]

Related

Replace part of uri with .htaccess

I want to replace get parameter with mod_rewrite in .htaccess. I have Url www.domain.at/success?id=12345 and need to replace "id" with "vid" -> www.domain.at/success?vid=12345
This replacement must only work on "success" page/uri, but not on other pages of website.
I tried
RewriteEngine On
RewriteRule ^(.*)success?id=([^0-9]*)$ /$1success?vid=$2 [R=301,L]
But this is not working on dynamic part?
Thanks for help!
Martin
You have to match query parameters in RewriteCond separately from request URI like this:
RewriteCond %{QUERY_STRING} ^id=(.+)$ [NC]
RewriteRule ^success/?$ /$0?vid=%1 [R=301,L,NC]

Changing url with .htaccess

I want to change URL displayed in browser depend of folder
www.domain.com/site1
www.domain.com/site2
I need to show
www.domain.com/site1 -> www.aaa.com
www.domain.com/site2 -> www.bbb.com
How it's possible to do that?
Many thanks
Use this code
RewriteEngine on
RewriteCond %{HTTP_HOST} www.domain.com/site1
RewriteRule ^(.*)$ http://www.aaa.com$1 [L]

Why is my pagination not working after mod rewrite?

Before rewriting my url file.php?style=foo&page=2 works fine.
After rewrite it stops working and just stays on the same page with the ?page variable at the end like it's not getting caught:
foo-new?page=2
After the rewrite the page parameter stops working on the clean url.
Here's what my rewrite rules look like:
RewriteCond %{QUERY_STRING} ^style=([A-Za-z-]+)/?$
RewriteRule ^file\.php$ %1-new? [NS,R=301,L]
RewriteRule ^([A-Za-z-]+)-new/?$ file.php?style=$1&redirect=no [NS]
So I translate what you'd like to do then, I change your rules and write them. Please tell me if I'm right or wrong.
You'd like to check if in the query string you have an optional parameter "style".
RewriteCond %{QUERY_STRING} ^style=([A-Za-z-]+)/?$
What is the point of the ^ and the /? and the $ ? Remove them:
RewriteCond %{QUERY_STRING} style=([A-Za-z-]+)
It's clearer this way and should still work for what you want.
RewriteRule ^file\.php$ %1-new? [NS,R=301,L]
What is the point of the ? ? Remove it:
RewriteRule ^file\.php$ %1-new [NS,R=301,L]
So, please try these rules and tell me if they work, and if not, please be more specific or give real URL sample that you'd like to rewrite:
RewriteCond %{QUERY_STRING} style=([A-Za-z-]+)$
RewriteRule ^file\.php$ %1-new [QSA,NS,R=301,L]
RewriteRule ^([A-Za-z-]+)-new/$ file.php?style=$1&redirect=no [NS]

.htaccess sub domain as file parameter with all the variables

I want to make this url redirect using .htaccess (linux/apache) ..
I want to keep "as is" all calls for www sub domain like this
www.example.com/index.php
www.example.com/test.php?id=1&name=a
www.example.com/whatever.php?whateverurl=xxxx
but I want to redirect all wild card urls like this with the sub domain as extra variable to current unlimited-whatever url variables
for example
xxx.example.com -> example.com/?subdomain=xxx
yyy.example.com/index.php -> example.com/index.php?subdomain=yyy
whatever.example.com/test.php?var=1 -> example.com/test.php?subdomain=whatever&var=1
whatever.example.com/whatever.php?var1=1&var2=2 -> example.com/whatever.php?subdomain=whatever&var1=1&var2=2
is this possible?
Thank you
Once you set up your Wildcard DNS Record you should make your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example\.com$
RewriteRule ^(.*)\??(.*)?$ $1?host=%{HTTP_HOST}&%{QUERY_STRING} [L] [L]
This rule works, although it will provide you the whole host, rather than just the subdomain, which you'll have to parse in your PHP.
Thank you for your answer
this rule seems to works
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^/?(.*)$ $1?subdomain=%1 [QSA,L,NE]
for the time without problems ...
thanks again for your answer
Best regards
Vangelis

.htaccess redirect "domain.tld/" to "domain.tld/home.html"

I have a website here on my localhost :
http://localhost/mysite/www/index.php
I have some RewriteRules to redirect like this :
http://localhost/mysite/www/index.php?page=home
-> http://localhost/mysite/www/home.html
And now, I want to do a redirection like this :
http://localhost/mysite/www/
-> http://localhost/mysite/www/home.html
I have an environment variable named REWRITE_BASE containing /mysite/www/. So what I thought to do was to compare {REQUEST_URI} to %{ENV:REWRITE_BASE} ... like this:
RewriteCond {REQUEST_URI} =%{ENV:REWRITE_BASE}
RewriteRule . %{ENV:REWRITE_BASE}home\.html [R=301,L]
But it don't works well.
To help you understand what I want to do, here is the working code in PHP to do what I want:
$rewriteBase = getenv('REWRITE_BASE');
if ($rewriteBase === $_SERVER['REQUEST_URI'])
header('Location: '.$rewriteBase.'home.html');
Thanks for help.
Okay... so, as I can't use a variable for my comparison, here is the way that I made it works :
# Redirect domain.tld/ to domain.tld/home.html
RewriteCond %{REQUEST_URI} =/www/ [OR]
RewriteCond %{REQUEST_URI} =/mysite/www/
RewriteRule ^(.*)$ %{REQUEST_URI}home\.html [R=301,L]
# RewriteBase equivalent - Production environment
RewriteRule . - [E=REWRITE_BASE:/www/]
# RewriteBase equivalent - Development environment
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule . - [E=REWRITE_BASE:/mysite/www/,E=DEVELOPMENT_ENV:1]
# Website rewritings
RewriteRule ^([^/]*)(?:/([^/]*))?\.html$ %{ENV:REWRITE_BASE}index\.php?page=$1&view=$2 [QSA,L]
Now it's alright. Thanks for your answers! ;)
What you want to do won't work, because mod_rewrite doesn't expand any variables present in its test patterns. Therefore, when you do something like this...
RewriteCond %{REQUEST_URI} =%{ENV:REWRITE_BASE}
...What you're actually doing is comparing the value of %{REQUEST_URI} to the string "%{ENV:REWRITE_BASE}", instead of the value "/mysite/www/" like you wanted. Is there a reason that you can't specify the value directly in your RewriteCond or simply move the rules so that they're relative to the /mysite/www/ directory to begin with?
There might be another way to approach the problem, but unfortunately (and for some good reasons) you cannot perform that kind of comparison using mod_rewrite.

Resources