How to append two urls together via rewrite? - .htaccess

I'm a complete rewrite newbie and I'm lost. We need to pass a URL and append it to the end of another URL. For example: If I go to www.site1.com/x_x_x/xxx/xxxxxxxxx/file.pdf I need it to be passed and appended to a 2nd URL. It should look like this: www.site2.com/?remoteurl=www.site1.com/x_x_x/xxx/xxxxxxxxx/file.pdf
This is what I have so far: RewriteRule /([a-z0-9-]+)([a-z0-9-]+)([a-z0-9-]+)/([0-9]{3})/([0-9]{9})/([a-z0-9-]+)([a-z0-9-]+)([a-z0-9-]+)[A-Z]{2}.pdf$ https://site2.com/?remoteUrl=http://site1.com/(missing code goes here?) [QSA,NC,R]
The first part works perfectly. Any file I go to will match correctly and get forwarded to the second URL. Obviously I'm missing the crucial part of appending. I feel like an idiot for not being able to figure this out. I've tried a few stuff I found online but nothing is working for me.
I'm not sure if that's even the correct way of doing this.

You can use this :
RewriteEngine on
RewriteRule ^ http://example2.com?remoteurl=%{HTTP_HOST}%{REQUEST_URI} [L,R,NE]

Related

Dont show $_GET variable in URL with .htaccess

forum_id forum_name
1 stuff
2 stuff
3 stuff
i have above table forum. forum.php calls it like forum.php?forum_id=? Now i have written a mod_rewrite for forum.php
RewriteRule ^forum/([0-9]+)$ forum.php?forum_id=$1 [L]
After that my url looks like example.com/forum/4. However i want my url to show forum_name not forum_id like example.com/forum/stuff. What can i do for this thing. I am gonna get forum table with forum_id because its the right method. But this time how i am gonna know what is writing inside stuff, and show it to user? I dont know if i can explain it correctly. Thanks for help
First you need to change your matching pattern:
# Match anything after "forum/" that's not a slash
RewriteRule ^forum/([^/]+) forum.php?forum_name=$1 [L]
And after that of course you need to change the database query in your PHP to select the forum by forum_name instead of forum_id.

htaccess redirecting old urls

I'm working on my htaccess and I finally got a result I can deal with. First I've got this
RewriteEngine on
RewriteBase /
RewriteRule ^blog/([a-zA-Z0-9\-/\.]+)/?$ blog.php?id=$1article_title=$2 [L]
This helps show my urls like so:
https://www.mydomain.com/blog.php?id=10
To
https://www.mydomain.com/blog/10/title.com
https://www.mydomain.com/blog/10/
https://www.mydomain.com/blog/10
https://www.mydomain.com/blog/10/title.com/
Four things here...
First is that all of these are accessible. I only want the first to appear and the others to direct there.
Second is that the article title will have spaces. Is there a way to turn spaces into - rather than have spaces?
Third is...is it okay to have .com within the url at the end? It just happens to be part of a post article title.
Fourth is do I include the extra part of the url$article_title in the urls I use? I'm lost on that part. Is it the same as I did with the id?
Last I was curious to know if I needed the id to show up. Can I exclude that in some way?
If I have urls already in place that use blog.php?id=10 do I change that to the new one? If I can't find it I'd like the url to go to the first rewritten url. All of the possible examples should go to that first url. or one without the id.

Trying to make a GET variable invisible in an URL but retain its usefulness using mod_write

Good day all,
I am trying to master the ,magic of mod_rewrite and require some advice/help.
I am trying to turn an URL from:
http://www.domainname.com/preview/about/5
To this:
http://www.domainname.com/preview/about
The issue is, I still need to retain the [id] part of the original URL to be used as a GET later on and it not be visible.
The code I have thus far:
RewriteRule ^preview\/([^/]+)\/([^/]+)\/$ /preview\/$1?id=$2 [R=301,QSA]
RewriteRule ^preview\/([^/]+)\/$ ?mode=preview&id=$2 [L,QSA]
This manages to create an URL like: http://www.domainname.com/preview/about/?id=5 and passes the ID through, I just need the ?id=5 to be invisible in the URL.
Thank you in advance anyone who has a solution for this, much appreciated.
UPDATE:
I have managed to get the following code to work as expected alas this is using static values for ID all I now need for this to be complete is to get it working off dynamic values for ID.
RewriteRule ^preview\/([^/]+)\/([^/]+)\/$ /preview\/$1 [R=301,QSA]
RewriteCond %{QUERY_STRING} !.*id=5.*$
RewriteRule ^preview\/([^/]+)\/$ ?mode=preview&id=5 [L,QSA]
You cannot get 'invisible' get parameters. The closest you'll get is setting a cookie to pass this data onwards.
RewriteRule ^preview/([^/]+)/([^/]+)[/]?$ preview/$1/ [CO=id:$2:127.0.0.1:1:/preview/$1:0:1,R]
In php you can access this cookie with $_COOKIE['id'] and the id is invisible in the url (because it isn't actually there). Documentation about the CO flag can be found here.
Edit: If you want to do it all with mod_rewrite, you can access this cookie from mod_rewrite too. As this is an internal rewrite, you can probably just use a direct path to the actual file you want to call.
RewriteCond %{HTTP_COOKIE} id=([^;]*)
RewriteRule ^preview/([^/]+)[/]?$ preview/$1?id=%1 [CO=id:-:127.0.0.1:-1:/preview/$1:0:1,END]
Edit2: I've added in a reset for the id-cookie in the second rule (expiry time T-1 minutes). This will cause the correct page to load if the user decides to go to preview/about/ again within 1 minute from going to preview/about/5 (which redirects to preview/about/ with a hidden id set to '5' to load something different).
If you are not passing the "ID" as part of the query string (e.g. ?id=5) or part of the URI (e.g. /preview/about/5) then you need to pass it in the request body, in something like a POST request. Otherwise, you can't make it "invisible", because the webserver isn't going to see it. If the webserver doesn't see it as a request, there is nothing mod_rewrite can possibly do to extract it.
Assuming you can't setup your site so that requests get POSTed (sort of like how a form is submitted) everytime someone clicks on a link, you're best bet is probably having it look like the http://www.domainname.com/preview/about/5 form, or maybe http://www.domainname.com/preview/about-5?

redirect old wordpress ?page_id= to non-wordpress site

I used to have a WP site that I converted to a standard html site. Problem is I found doing a google search that instead of http://www.genealogyinc.com it was returning http://www.genealogyinc.com/?page_id=21, I dont know how many pages are like this but am trying to find a htaccess workaround, all the ones I found online give me 500 server errors.
Need a rewrite for any ?page_id= cause I dont know how many other numbers are out there.
Thanks
Off the top of my head, without testing, it would be something like this.
The first line looks for the page_id querystring parameter, and if it meets it, it should pass on to the second line. The rewrite rule I have below may need some tweaking, but I hope this helps you.
RewriteCond %{QUERY_STRING} page_id=(.*)$
RewriteRule $ /? [R=301,L]

.htaccess redirect/rewrite help needed

I am writing htacess redirect rule but it is not working properly I have tried many solutions but simply it is not working.
What I want to do is to
I have url http://example.com/cms/index.php?page=filename I want this url to be executed and show appropriate page but in browser it should show example.com/cms. And what is important is I only want to right this rule for this page only and it should not effect to other pages.
Thank you.
RewriteRule ^([^/]+)/$ /cms/index.php?filename=$1 [L,QSA]
The L at the end says it is the last rule (stop processing) and QSA means 'Query String Append', so if someone puts other parameters after it, such as:
http://example.com/cms.htm?order=desc
The GET value for order will be passed also - without it it'll just quietly drop it.
Something like this ought to work:
RewriteEngine on
RewriteRule ^http://example.com/cms$ http://example.com/cms/index.php?page=filename
...should work.
Have a look at a tutorial with some examples if you're interested in seeing what else you can do.

Resources