php .htaccess create subdomain excluding query string - php

I have following records in .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(^.*)\.example\.com
RewriteRule ^([0-9].*)$ /folder/%1/$1 [L,NC,QSA]
What I want is:
Show example.com/folder/user/0's content by accessing user.example.com/0
When I access user.example.com/0 my link looks like: user.example.com/folder/user/0
How can I fix it?

Here's something that would take any URL of the form user.example.com/N and make it into a redirect to example.com/folder/user/N
RewriteCond %{HTTP_HOST} ^(^.*)\.example\.com
RewriteRule ^([0-9]+)$ http://example.com/folder/%1/$1 [L,NC,QSA]
The redirect happens because of the change of host. This means the user's browser bar will become example.com/folder/user/N

Related

How to rewrite an url with two query inside to one folder?

I'm facing a problem to transform my actual url
http://website.com/login/profil.php?id=34&pseudo=robin
into this one :
http://website.com/myspace
I checked if the rewrite engine works well and it's ok so that's my .htaccess
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteCond %{QUERY_STRING} ^&pseudo=([a-zA-Z0-9]+)$
RewriteRule ^myspace\$ http://website/login/profil.php?id=$1&pseudo=$2 [R=301,L]
So my wish is to redirect the user's space on one common directory. what's the best way do do this ? am i wrong with the query ?
Thanks in advance guys !
This code doesn't work :
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^test\.html$ /profil.php?id=([0-9]+)&pseudo=([a-z]+) [L]
Try this .htaccess it will work only when get parameters are in the same order i.e.
id=32&pseudo=code
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/login/profil\.php$
RewriteCond %{QUERY_STRING} ^id=(\d+) [NC]
RewriteCond %{QUERY_STRING} &pseudo=(\w+) [NC]
RewriteRule ^login/profil\.php$ /myspace? [L,R=301]
This format works for me. You also try this:
Put this line into your .htaccess file
RewriteRule ^myspace/?$ login/profil.php?id=34&pseudo=robin
And yes, do not forget to set rewrite base in your .htaccess file:
below line will set the base url:
RewriteBase /
So, basically you need to put these both lines into your htaccess file
RewriteBase /
RewriteRule ^myspace/?$ login/profil.php?id=34&pseudo=robin
For dynamic id and name, you can use:
RewriteRule ^myspace/([0-9]+)/([A-Za-z0-9._-]+)/?$ login/profil.php?id=$1&pseudo=$2
So, into your profile.php file, you can check parameter values(id and name)by
print_r($_REQUEST);
So, example, you will have to run the url something like,
http://websitename.com/myspace/20/robin/ or
http://websitename.com/myspace/25/john/ ... and like wise.

How to rewrite URL and return `404` for original links?

I created my website with some files in htdocs folder, such as: .htaccess, web.php ... In past, I used an old URL structure for my website: old.php?id=12. Then, I have used a new one: new/12, by using this simple .htaccess:
RewriteEngine On
RewriteRule "^new/([0-9]+)/?$" "/old.php?$1"
And now, I do not want any user can access my website with old URL structure. Could you show me: How to rewrite URL and return 404 for original links?
Try this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^new/(\d+)*$ ./old.php?id=$1
The best solution, especially for SEO, is to make a redirect 301:
RewriteEngine on
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+old\.php\?id=(\d+) [NC]
RewriteRule ^ /new/%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^new/(\d+)/?$ /old.php?id=$1 [L,QSA,NC]
To get a real 404 effortlessly, the simplest is to change your hidden page name to new.php (also in htaccess) and delete the old.php page.

Using RewriteRule shows apache folder navigation or 404, why?

I have wordpress installed and in the root i created a folder and put an .htaccess inside to redirect to another web page without changing the URL.
This was working fine until i move to another server (the older one was just horrible) and now it just not working.
This is code that i was using:
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/conexionalam
RewriteRule ^(.*)$ http://nuevoserver.breinguash.com/$1 [R=301,L,P]
and is no longer working, show me this:
looking for internet i have tested some other codes:
1-
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^/conexionalam(/.*|)$ nuevoserver.breinguash.com$1 [L,NC]
2-
RewriteEngine ON
RewriteRule ^/(.+) http://nuevoserver.breinguash.com/$1 [R,L]
This two show me this:
3-
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://nuevoserver.breinguash.com/$1
This one redirect me to http://nuevoserver.breinguash.com/index.html
I realized that if i took off this '[R=301,L,P]' at the end of the first code, it works but changing the URL and i don't want tat.
Thanks in advanced!
EDIT:
By the moment im using this code:
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/conexionalam
RewriteRule ^(.*)$ http://nuevoserver.breinguash.com/$1
Its redirects but change the URL = /
but for now does not affect google analytics.
if i change or add something to the code it does show me the errors from above. i really doesn't understand why it does that.
SOLUTION
Well it seem that i cant redirect between domains/subdomains without changing the URL just like that, i have to do it with proxy, so the solution was:
Enable proxy and proxy-http on apache.
Use the P flag for RewriteRule
So right now i'm using this code and it works perfectly:
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/conexionalam
RewriteRule ^(.*)$ http://nuevoserver.breinguash.com/$1 [R=301,L,P]
Give this a shot in your .htaccess file...
RewriteEngine On
RewriteBase /
RewriteRule ^conexionalam(.*)$ http://nuevoserver.breinguash.com/$1 [L,R=301]
This should redirect you to http://nuevoserver.breinguash.com when you go to /conexionalam from your main domain.

Htaccess Redirect Rewrite Not working together

I want to remove the query strings from the urls using Htaccess, I used the following code for changing the urls, It did but after redirection to that url, I am getting 404 error.
and there is rewrite statement also if i use that only, then the new url works without 404 error, but the old urls doesnt get automatically redirected to new urls.
Here is the htaccess and url the I am modifying
Options FollowSymLinks
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/profile\.php$
RewriteCond %{QUERY_STRING} ^user_id=([0-9]*)$
RewriteRule ^(.*)$ http://www.meenmipage.com/user/%1? [R=302,L,NC]
Actual Url was:
http://www.meenmipage.com/profile.php?user_id=2
and modified was:
http://www.meenmipage.com/user/2
If i remove the above code and just use the rewrite statement as like this one:
RewriteRule ^user/([^/]*)$ /profile.php?user_id=$1 [NC,L]
Then the new modified url works and the old one also works
Please tell me what to do?
I think you need to remove $ at the end of this line:
RewriteCond %{REQUEST_URI} ^/profile\.php
because if the request URI has user_id=2 then this condition will not match
Try This !
-----------
Options -MultiViews
Options +FollowSymlinks
RewriteEngine on
rewritecond %{QUERY_STRING} ^user_id=([0-9]*)
rewritecond %{http_host} ^www.meenmipage.com [nc]
RewriteRule ^([0-9]+)$ //www.www.meenmipage.com?user_id=$1 [L,QSA]

Send full url with query string to file in mod_rewrite

I want to catch every request that comes in on my domain name and redirect that url to a php file. In this PHP file I show a form and when the user passes the form I want to redirect the user to the page it requested orignally.
I've got the following mod_rewrite:
RewriteEngine on
RewriteCond %{HTTP_COOKIE} !(accepts_cookie) [NC]
RewriteRule ^(.*) test.php?link=.. [L]
As you see I would like to place the full requested url in the link= but how am I able to do this? I tried stuff like: REQUEST_URI, but that doesn't give me the full path including query strings.
I hope someone can help!
Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Options +FollowSymLinks
RewriteCond %{HTTP_COOKIE} !(accepts_cookie) [NC]
RewriteRule ^(.*)$ test.php?link=%{REQUEST_URI}%{QUERY_STRING} [L]
</IfModule>
You can always consider the mod_rewrite Cheat Sheet.

Categories