htaccess to remove the GET params, but still pass the value - apache

I currently have it set to redirect www.foo.***/2 to www.foo.***/index.php?id=2, but i am trying to get it to redirect without displaying the URL.
I want the URL to stay as www.foo.***/2 but atually deliver www.foo.***/index.php?id=2
my htaccess file:
Options +FollowSymlinks
RewriteEngine ON
RewriteRule ^$ h**p://server/~user/folder/sub/index.php [L]
ErrorDocument 404 h**p://server/~user/folder/sub/ERROR/
ErrorDocument 500 h**p://server/~user/folder/sub/SERVER/
RewriteRule ^(error)/([0-9A-Za-z+]+)?$ h**p://server/~user/folder/sub/index.php?action=ERROR [L,NC]
RewriteRule ^(server)/([0-9A-Za-z+]+)?$ h**p://server/~user/folder/sub/index.php?action=SERVER [L,NC]
RewriteRule ^([0-9A-Za-z+]+)?$ h**p://server/~user/folder/sub/index.php?id=$1 [L,NC]
any help would be great thanks!
bo huttinger
thanks to awesome spamz protectionz, I subsituted stars for letters in the post

It was a 'same domain' issue. If you call a location on the same server, it doesn't rewrite the URL in the bar
solution: instead of using:
h**p://server/~user/folder/sub/index.php?id=$1 [L,NC]
use this:
/home/~user/folder/sub/index.php?id=$1 [L,NC]
hope this helps!

Related

Htaccess Mod Rewrite Not working for Pretty URL

I feel like such a tool for having to post this question, but for the life of me, I cannot figure out how to resolve my issue. (I've also read/tried previous posts but none have helped me)
I'm trying to turn https://mywebsite.com/article.php?slug=pretty-url to https://mywebsite.com/article/pretty-url
The problem I'm having is the $_GET method is not recognizing the slug, so it's giving me a 404 error. The slug is definitely in my database. I'm not sure why I cannot retrieve it.
Below is my htaccess code and my php code to call the page.
Htaccess Code:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
# Redirect www urls to non-www
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com [NC]
RewriteRule (.*) https://mywebsite.com/$1 [L]
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://mywebsite.com/$1 [L]
ErrorDocument 404 /404.php
#Pretty URL for Blog
RewriteRule ^article/([0-9a-zA-Z]+) article.php?slug=$1
#Rewrite for certain files with .php extension
RewriteRule ^about$ about.php
RewriteRule ^services$ services.php
RewriteRule ^portfolio$ portfolio.php
RewriteRule ^blogs$ blogs.php
RewriteRule ^tutorials$ tutorials.php
RewriteRule ^contact$ contact.php
RewriteRule ^privacy-policy$ privacy-policy.php
RewriteRule ^terms-of-service$ terms-of-service.php
RewriteRule ^sitemap$ sitemap.php
</IfModule>
PHP Code on the article.php page:
//Get the blog. Only blogs that are published
$slug = $_GET['slug'];
$publish = intval(1);
//Query the database
$sql = "SELECT * FROM blogs WHERE publish = $publish AND slug = $slug";
//Execute the query
$stmt = $db->query($sql);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
([0-9a-zA-Z]+) will not capture pretty-url because the group doesn't allow for hyphens. Change that to ([A-Za-z0-9-]+) and add [L] to the end of that line.
Also, for the sake of doing things properly, remove the second and third calls to RewriteEngine On.
Never apologise for a question! If you're stuck, we will try to help.
What you need in your htaccess is the following:
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /article.php?slug=$1 [L]
That should change your url to https://mywebsite.com/pretty-url.html

Where is my mod rewrite fail?

I have got url
ipaddr/opensys/base/
and some other similar, like
ipaddr/opensys/base/something.php?olol=yep
I want to remove from url "opensys" and display there "center", i want to see this links working:
ipaddr/center/base/
and
ipaddr/center/base/something.php?olol=yep
I did it with symlinks, but it was not good, because system is very difficult and with symlink some plugins not works, I want to do it with .htaccess only, after it all will be ok.
My htaccess is:
but links, which i want to see is not working, why?
RewriteEngine on
RewriteRule ^/opensys/base/(.*)$ /center/base/$1 [L]
RedirectMatch 301 ^/opensys/base/(.*)$ /center/base/$1
Redirect works good, but i see 404, rewrite rules is not working. Why?
You also need to handle the /center/base/ now in the .htaccess file as you must be handling opensys earlier. Something like this -
RewriteRule ^/opensys/base/(.*)$ /center/base/$1 [R=301]
RewriteRule ^/center/base/(.*)$ /index.php?args=$1 [QSA,L]
You can use this code in root .htaccess:
RewriteEngine on
RewriteCond %{THE_REQUEST} /opensys/(base/\S*)\s [NC]
RewriteRule ^ /center/%1 [R=301,L,NE]
RewriteRule ^center/(base/.*)$ opensys/$1 [L,NC]

Apache htaccess sending URLs in a URL

I'm trying to send a URL in a URL, and there's ModRewrite in the middle.
Here's the Rewrite Rule. It's used to search for a user's profile.
RewriteEngine On
RewriteRule ^(query)/(profile)/([a-z0-9:.()\-\ ]+)/?$ search/index.php?scope=$2&query=$3 [NC,L]
And this is the URL
http://www.example.com/query/profile/http://www.facebook.com/example
No matter how I do the rewrite rule, I keep getting NetworkError: 500 Internal Server Error or NetworkError: 404 Not Found if it's a URL I put there.
How can I do this right? I'm on Windows 7 Home Premium. Am I missing something?
The URL above is passed using ajax.
Edit:
#htaccess in site1 folder
RewriteRule ^resources/?(.*)/?$ /control/$1 [NC,L]
#htaccess in control folder
RewriteRule ^(query)/(profile)/([a-z0-9:.()\-\ ]+)/?$ search/index.php?scope=$2&query=$3 [NC,L]
next folder is `search` which has `index.php`
URL used
http://www.example.com/resources/query/profile/http://www.facebook.com/example
EDIT:
I redid the RewriteRule like below, and don't get any error messages now, but Apache seems to be taking off one / from the url after the http://. So I get only http:/www.facebook.com/example. Any help with that will be appreciated.
RewriteRule ^(query)/(profile)/([a-z0-9\/:.()\-\ ]+)/?$ search/index.php?scope=$2&query=$3 [NC,L]`
You need to capture URL from RewriteCond otherwise Apache will trim multiple // into a single one.
Change this rule in root .htaccess:
RewriteCond %{REQUEST_URI} /resources/(.+)$ [NC]
RewriteRule ^ /control/%1 [L]
Have this in /control/.htaccess file:
RewriteEngine On
RewriteBase /control/
RewriteCond %{REQUEST_URI} /(query)/(profile)/(.+)$ [NC]
RewriteRule ^ /resources/search/index.php?scope=%2&query=%3 [QSA,L]

Apache mod_rewriting not finding any files

OK, so we have a specific set of rewrites that we use on a number of sites due the file structure of our programming. It's never been a problem before until now.
For example, the rewrite:
RewriteRule ^$ Pages/news.php
Instead of directing to http://www.domain.com/wedding-venues-and-caterers-news/Pages/news.php it is redirecting to /path/to/file/public/wedding-venues-and-caterers-news/Pages/news.php and it's 404-ing (/path/to/file being the actual path).
If anyone could shed any light as to why this is happening, or point me in the right direction then I would be eternally grateful.
I am running all of my script and files in a subfolder, there is another .htaccess file in the root folder which I have edited in at the bottom but it doesn't seem to contain anything that could be interfering.
If you require any further information on the server then let me know!
EDIT - Here are all my rewrites as requested.
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteRule ^$ Pages/news.php
RewriteRule ^images/([0-9]+)/([0-9]+)/(0|1)/(.*).jpg?$ Classes/Image/timthumb.php?src=http://www.domain.com/wedding-venues-and-caterers-news/images/$4.jpg&h=$1&w=$2&zc=$3
RewriteRule ^([A-Za-z0-9\-]+)/([0-9]+)/([0-9]+)(/)?$ Pages/archives.php?cat_id=$2&page=$3 [NC,L]
RewriteRule ^archives/mon/([0-9]+)/yr/([0-9]+)/([0-9]+)$ Pages/archives.php?mon=$1&yr=$2&page=$3 [NC,L]
RewriteRule ^([A-Za-z0-9\-]+)/([0-9]+)(/)?$ Pages/archives.php?cat_id=$2 [NC,L]
RewriteRule ^archives/mon/([0-9]+)/yr/([0-9]+)(/)?$ Pages/archives.php?mon=$1&yr=$2 [NC,L]
RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/([0-9]+)(/)?$ Pages/article.php?art_id=$3 [NC,L]
RewriteRule ^wedding-venues-catering-rss(/)?$ rss/rss.php [NC,L]
RewriteRule ^Page-Error(/)?$ Pages/errorPage.php [NC,L]
ErrorDocument 404 http://www.domain.com/wedding-venues-and-caterers-news/Page-Error
ErrorDocument 401 http://www.domain.com/wedding-venues-and-caterers-news/Page-Error
ErrorDocument 403 http://www.domain.com/wedding-venues-and-caterers-news/Page-Error
ErrorDocument 404 http://www.domain.com/wedding-venues-and-caterers-news/Page-Error
ErrorDocument 500 http://www.domain.com/wedding-venues-and-caterers-news/Page-Error
I also tried to reference the rewrites with absolute URL's, this worked of course, but they were just redirecting instead of rewriting as you might expect.
EDIT - This is the other .htaccess file in the root directory, which is part of their existing site.
#Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
<IfModule mod_rewrite.c>
RewriteEngine on
redirect 301 /Cavendish.htm http://www.domain.com/
redirect 301 /grange.htm http://www.domain.com/
redirect 301 /restaurant/index.html http://www.domain.com/
redirect 301 /news/Save-money-by-booking-wedding-venues-in-Middlesex-at-off-peak-times.asp http://www.domain.com/
redirect 301 /news/default.asp http://www.domain.com/
EDIT - I thought this might be relevant.
The 404 error on the rewrite shows this path -
/l/i/domain.com/public/wedding-venues-and-caterers-news/Pages/news.php
But.. if I edit news.php and request SCRIPT_FILENAME it gives me the path..
/services/webpages/l/i/domain.com/public/wedding-venues-and-caterers-news/Pages/news.php
My guess is that something is affecting the default path.
Furthermore I have tried basic rewrites at root level, but these also 404.
Finally the issue has been solved.
As the server is hosted with BT Business I decided to email them to see if they could help.
A guy replied saying he had logged in to the server and modified the .htaccess and it now works fine.
He simply added the subfolder in front of the rewritten URL's.
IE:-
RewriteRule ^$ Pages/news.php
Became: -
RewriteRule ^$ wedding-venues-and-caterers-news/Pages/news.php
I am still unsure why the subdirectory is needed when the .htaccess file is inside that directory, my guess is it is something to do with their server settings.

htaccess redirect problem

I'm trying to get my .htaccess to redirect all requests to the /blogs directory, except for requests that start with /staff. This is what I have so far:
RewriteEngine On
RewriteRule ^staff(.*)$ staff/$1 [L]
RewriteRule ^(.*)$ blogs/$1 [L]
It works fine for every case, except when I do http://mydomain.com/staff or http://mydomain.com/staff/alice in which case I get a 500 Internal Error.
What am I doing wrong?
Try this:
RewriteRule ^staff/ - [L]
RewriteRule !^blogs/ blogs%{REQUEST_URI} [L]
What is logged to error.log? The error may be coming from either mod_rewrite or from your application itself.