LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-21-2012, 09:48 AM   #1
ChristianLJ
LQ Newbie
 
Registered: Jul 2012
Posts: 19

Rep: Reputation: 0
Smile What to use? Alias, Redirect, Rewrite?


Hello.

I am trying to get www.domain.com/NAME to redirect to www.domain.com/a/NAME.
"NAME" should be able to be anything between [a-z] and [0-9].
Furthermore must this redirect only happen if the URL doesn't exist. On the www.domain.com is a Wordpress page.
So if www.domain.com/contact exists the redirect should not happen.

It would be best if this was possible with Alias​​, so I could mask the URL, so www.domain.com/NAME didn't change the URL, but would show the content from www.domain.com/a/NAME, or even better a directory on the server.

Is this possible?

Best Regards, Christian.
 
Old 10-21-2012, 11:44 AM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 12,196

Rep: Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676
Hi,

Quote:
Furthermore must this redirect only happen if the URL doesn't exist. On the www.domain.com is a Wordpress page.
So if www.domain.com/contact exists the redirect should not happen.

It would be best if this was possible with Alias​​, so I could mask the URL, so www.domain.com/NAME didn't change the URL, but would show the content from www.domain.com/a/NAME, or even better a directory on the server.
If you want a conditional redirect and mask the actual URL, then you should use mod_rewrite.
You can try something like that:
Code:
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/a
RewriteRule (.*) /a/$1
Regards

Last edited by bathory; 10-21-2012 at 11:59 AM. Reason: typos
 
Old 10-21-2012, 12:31 PM   #3
ChristianLJ
LQ Newbie
 
Registered: Jul 2012
Posts: 19

Original Poster
Rep: Reputation: 0
Hi bathory.

I am aware of the RewriteRules, but will your solution work?
What will happen if i visit www.domain.com/NAME and it exists in Wordpress?
 
Old 10-21-2012, 12:37 PM   #4
ChristianLJ
LQ Newbie
 
Registered: Jul 2012
Posts: 19

Original Poster
Rep: Reputation: 0
If i try your solution bathory, i get a 403 on www.domain.com
I have the following in my domain.conf
<VirtualHost *:80>
ServerAdmin admin@domain.com
ServerName domain.com
ServerAlias www.domain.com
Alias /demo /srv/domain/demo
Redirect permanent /admin https://www.otherdomain.dk/admin
DocumentRoot /srv/www/domain.com/public_html/
ErrorLog /srv/www/domain.com/logs/error.log
CustomLog /srv/www/domain.com/logs/access.log combined
DirectoryIndex index.php
<Directory "/srv/www/domain.com/public_html">
AllowOverride None
Options None
Order allow,deny
Allow from all
Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</Directory>
</VirtualHost>
 
Old 10-21-2012, 01:43 PM   #5
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 12,196

Rep: Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676
Quote:
What will happen if i visit www.domain.com/NAME and it exists in Wordpress?
You'll see it

Quote:
If i try your solution bathory, i get a 403 on www.domain.com
A 403 error (Forbidden) means that you are redirected to a URL without an index page (if you don't the Indexes options set), or with wrong permissions for the apache user.

BTW your rewrite rules, rewrite the non existing URLs to the index.php page.
From your OP I don't think this is what you actually want, as you mentioned that you want a not existing /NAME URL to be rewritten to /a/NAME URL
 
Old 10-21-2012, 01:49 PM   #6
ChristianLJ
LQ Newbie
 
Registered: Jul 2012
Posts: 19

Original Poster
Rep: Reputation: 0
Ok, thank you. In the meantime i have found a (maybe) better solution.

I am using RewriteCond and RewriteRule to get the parameters from a wildcard in www.*.domain.com

RewriteCond %{HTTP_HOST} ^(www\.)?([^.]*)\.domain\.dk$
RewriteCond %{HTTP_HOST} !^www\.domain\.dk
RewriteRule (.*) https://www.domain2.dk/?id=%2 [R] [NC]

This works, but changes the URL. Is it possible to mask the URL, so it looks like it is on domain.com? Both domains are on the same server.
 
Old 10-22-2012, 12:05 AM   #7
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 12,196

Rep: Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676
Quote:
RewriteRule (.*) https://www.domain2.dk/?id=%2 [R] [NC]

This works, but changes the URL. Is it possible to mask the URL, so it looks like it is on domain.com? Both domains are on the same server.
It changes the URL because it's an external rewrite.
If you want to mask the host, use [P] instead of [R] above, or better yet change all this to a reverse proxy scenario

Regards
 
Old 10-22-2012, 09:19 AM   #8
ChristianLJ
LQ Newbie
 
Registered: Jul 2012
Posts: 19

Original Poster
Rep: Reputation: 0
Ok. If i change the flag, it doesn't change anything in the url.

Therefore i am now trying to approach this in a different way.

I have created an Alias that points www.domain.com/b downto the directory where the index.php file are.
Then i have used RewriteRule and RewriteCond to take the wildcard, example www.example.domain.com and change this to www.example.domain.com/b?cvr=123456789
This is the closest i can get to an solution which is acceptable, but not optimal.

I would like it to hide the 'extension' from the url www.123456789.domain.com/b?cvr=123456789

Is this possible? I have read about mod_proxy, but it doesn't seems like i can use it to archieve this.

I have included my domain.conf file.

PHP Code:
<VirtualHost *:80>
     
ServerAdmin [email]support@domain.com[/email]
     
ServerName domain.com
     ServerAlias 
[url]www.domain.com[/url]
     
ServerAlias *.domain.com
     Alias 
//srv/dir/folder
     DocumentRoot 
/srv/www/domain.com/public_html/
     
ErrorLog /srv/www/domain.com/logs/error.log
     CustomLog 
/srv/www/domain.com/logs/access.log combined
     DirectoryIndex index
.php
<Directory "/srv/www/domain.com/public_html">
AllowOverride None
Options None
Order allow
,deny
Allow from all
Options FollowSymLinks
RewriteEngine On
RewriteBase 
/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond 
%{REQUEST_FILENAME} !-d
RewriteRule 
. /index.php [L]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]*)\.domain\.com$
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule 
(.*) [url]http://www.%2.domain.com/b?cvr=%2[/url] [QSA,P,NC,L]
</Directory>
</
VirtualHost
Regards
 
Old 10-22-2012, 10:41 AM   #9
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 12,196

Rep: Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676
Quote:
Then i have used RewriteRule and RewriteCond to take the wildcard, example www.example.domain.com and change this to www.example.domain.com/b?cvr=123456789
This is the closest i can get to an solution which is acceptable, but not optimal.

I would like it to hide the 'extension' from the url www.123456789.domain.com/b?cvr=123456789
I'm afraid I cannot understand what you're trying to do. What you mean by "hide the extension"? What is the extension?
The last RewriteRule, rewrites everything from www.example.domain.com to www.example.domain.com/b?cvr=example
 
Old 10-22-2012, 10:44 AM   #10
ChristianLJ
LQ Newbie
 
Registered: Jul 2012
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
I'm afraid I cannot understand what you're trying to do. What you mean by "hide the extension"? What is the extension?
The last RewriteRule, rewrites everything from www.example.domain.com to www.example.domain.com/b?cvr=example
Sorry, for my bad english.

I am aware of the RewriteRule that rewrites www.example.domain.com to www.example.domain.com/b?cvr=example.

My question was: Is it possible to hide /b?cvr=example in www.example.domain.com/b?cvr=example?
 
Old 10-22-2012, 01:44 PM   #11
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 12,196

Rep: Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676Reputation: 1676
Quote:
My question was: Is it possible to hide /b?cvr=example in www.example.domain.com/b?cvr=example?
I guess it's possible, but it's beyond my mod_rewrite knowledge. You may search, or ask your question in webmasterworld

Regards
 
Old 10-22-2012, 03:21 PM   #12
ChristianLJ
LQ Newbie
 
Registered: Jul 2012
Posts: 19

Original Poster
Rep: Reputation: 0
Ok, i will try that.
Thank you for your time and help
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to rewrite/redirect directory to port number in apache? jiecenzhao Linux - Server 4 06-04-2011 05:24 PM
nginx rewrite / redirect LMW Linux - Server 4 05-14-2011 11:37 AM
mod rewrite redirect nuttynibbles Linux - General 4 06-19-2010 04:57 PM
Problem with dynamic url rewrite and redirect in nginx??? gavin2u Linux - Server 3 05-27-2010 10:25 PM
mod_rewrite: how to redirect but _not_ rewrite urentity Linux - Server 2 02-03-2009 06:02 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 12:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration