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

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.

Related

php .htaccess create subdomain excluding query string

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

How to redirect to another subdirectory with same name with .htaccess

I want to redirect my file in folder A to folder B with the same file name.
EG.
yourdomain.com/folderA/nosedigger.php
to
yourdomain.com/folderB/nosedigger.php
The .htaccess is at yourdomain.com/folderA/.htaccess.
I tried the following but it does not work like a charm. Need help. Totally have no clue what the codes in .htaccess really mean.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/folderB/ [NC]
RewriteCond %{REQUEST_URI} ^/(.*)$
RewriteRule ^ /folderB/$2 [R=301,L]
To redirect from foldera to folderb, you can use the following redirect above your other rules
RedirectMatch ^/foldera/(.*)$ /folderb/$1
this should be all you need:
RewriteEngine On
RewriteRule /folderA/(.*) /folderB/$1 [R=302,NC]
maybe:
RewriteRule ^folderA(/.*)?$ folderB$1 [R=302,NC]

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]

.htaccess causing $_GET to return NULL

I am trying to pass a variable from one page to another via my url. The structure of the urls looks like this http://localhost:8888/test_portfolio?location=ignite_rockford.
Here's the relevant info from my .htaccess file:
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %{REQUEST_FILENAME}.php?/$1?$1=$1
</IfModule></code>
The problem seems to be with this line: RewriteRule ^(.*)$ %{REQUEST_FILENAME}.php?/$1?$1=$1, but I don't know what to do. If I add .php after test_portfolio, then it works, but otherwise when I dump $_GET['location'] I get NULL.
Oh yeah, I'm retrieving $_GET with $location = $_GET["location"];.
Any help would be appreciated.
If you are just trying to remove the .php, you can just add it and copy the query string with [QSA] tag:
RewriteRule ^(.*)$ $1.php [QSA]
Your URLs will go from:
/test to /test.php?/test?test=test are you sure this is what you want? (it looks malformed, among other things)
Usually you'll write a Router like:
RewriteRule ^(.*)$ router.php?__url=$1 [QSA]
This will give you a GET parameter __url with the original path. [QSA] will allow you to still accept all original query parameters, like location.

Categories