PHP htaccess rewrite rules with wordpress - php

I have a running service that lets my user choose their own URL address (example: http://hsdfhdfghfh.com/theUserURL)
Now I'm going to put my front site on a wordpress but keep my app running along-side with it.
Now for the problem, i've set a rule in htaccess to forward my users url to their page, but wordpress also needs the same rule in order to make pretty URL (permalink).
Here is my htaccess with my code and the code that wordpress injected. Currently only my redirect is working, how can I make them both work together?
Can I add a code in my php file that gets the URLs and if it has no entries I throw it to wordpress to show?
This is my htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ ./url.php?w=$1
AddDefaultCharset UTF-8
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
And this is my php (url.php) file that handles the redirects:
<?php
session_start();
$url_name = $_GET['w'];
include ('init.php');
$result = mysql_query("SELECT * FROM urls WHERE url='$url_name'");
$num_rows = mysql_num_rows($result);
$query_result = mysql_fetch_assoc($result);
if($num_rows == 0){
include($theurl.'/404.php');
exit();
}
$_SESSION['url_id'] = $query_result['url_id'];
?>
..Shows the rest of the page for the user with the specified url_id session...
Maybe instead of showing the 404.php page I could transfer the entered URL to wordpress somehow?
Cheers!

After trying different approaches and trying to manipulate using .htaccess I realized that this process is very bad for loading times and overloading the server.
I ended up moving Laravel to a subdomain :/
Don't try to put Wordpress and Laravel together on the same domain unless you really want to tweak wordpress to the core.

Related

How to make an external Wordpress Main Page?

Actually I am running a Wordpress website with a simple template.
Now for SEO and Speed optimization purposes I would like to make a custom HTML page super simple and well optimized but that is not part of the Wordpress structure.
Basically, my own HTML page.
How can I have my Wordpress thinking the main page is that HTML created page as Main page and the rest of the website managed by the Wordpress ?
My answer requires an Apache or Litespeed server (and possibly others) with mod rewrite enabled, and where AllowOverride has not limited your use of .htaccess file.
See: https://codex.wordpress.org/htaccess#General_Examples
According to the WordPress documentation on that page, if you are using an .htaccess file to handle "pretty permalinks", then it creates and uses the following basic .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
What this code does is redirect all requests that are not for an actual file or directory to the index.php file. You can modify this easily and achieve what you want. Simply changing it to something like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp_index.php [L]
</IfModule>
# END WordPress
That change would redirect all requests to wp_index.php instead of index.php. So you would make that change, and then rename your index.php file to wp_index.php.
Once that is done, you can create a new index.php file and its contents can be plain HTML. Don't worry about the .php file extension. When somebody goes to your website, if they go to your homepage, the server will serve up the index.php file. For all other requests the server will use wp_index.php and WordPress will handle the request.
Please keep in mind that there are some differences in how .htaccess files work, depending on your server. If my changes don't immediately work, tweaking the .htaccess file contents may be necessary.

Changing the actual URL without changing display URL using htaccess [duplicate]

So, I've this problem:
Base Website located at http://example.com/
Second Website located at http://example.com/web2/
People making various requests to the second website like
http://example.com/myWeb/pg1 and http://example.com/web2/pg2
Recently and due to some other issues I need to have a custom new path for the second website but also keep the first one working.
The ideia is to allow users to access the second website over the two following addresses:
http://example.com/web2/
http://example.com/alternative-url-web2/
The folder /web2/ actually exists on the server, but how can I simulate the folder /alternative-url-web2/ and "redirect" the requests to /web2/?
Please note I don't want the URL on the browser to change, this must be a "silent redirect". And I also make sure that all other requests like http://example.com/other are not redirected by the second website.
Thank you.
Update:
According to #anubhava I could simply solve this issue by adding in my .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
This is probably working fine but I noticed the following:
http://ex.com/alternative-url-web2 is redirected to http://ex.com/web2/ (changing browser URL);
http://ex.com/alternative-url-web2/ is redirected to http://ex.com/(changing browser URL);
http://ex.com/alternative-url-web2/someRequest works fine and does NOT change the browser URL;
http://ex.com/alternative-url-web2/index.php works fine and does NOT change the browser URL;
Site Note:
At /web2/ there's an .htaccess that might be cause the wired redirect behavior above... So here is the file contents:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
Can the internal RewriteRule to index.php be causing all this? If yes, how can I fix it?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
Alternate code:
RewriteRule ^alternative-url-web2/?$ /web2/ [L,NC]
RewriteRule ^alternative-url-web2/(.+)$ /web2/$1 [L,NC]
This is a pretty simple rewrite. In the htaccess file in your document root, just add the following:
RewriteEngine On
RewriteRule ^alternative-url-web2/?(.*)$ /web2/$1 [L]
Unlike a redirect, which makes the browser/client send a new request for a new URL (thus changing what's in the browser's location bar), a rewrite happens entirely on the server's side.
By the way, in order to follow the trail of htaccess redirects, you could add something like this to each of them:
Header add X-Remark-Rewrite "/path.to/htaccess"
You can inspect these in the response in the developer tools.

Rewrite URLs for SEO friendly with .htaccess on Wordpress

I'm developing a website using Wordpress (for articles, static pages, ...) but in my theme folder I created a subsystem (handled via php-mysql that doesn't require Wordpress) that uses another database (that contains products). The php page of a single product (product.php) is this:
<?php
...
if($_GET['url']){
$url = $_GET['url'];
$sql = "select * from `products` where url='$url'";
$row = // start the query ($sql);
$title=$row[0]['title'];
$body=$row[0]['body'];
}
else{echo '404 Page.';}
?>
<body>
<?php
echo "<h1>$title</h1>
<p>$body</p>";
?>
</body>
And it's rewritten by Wordpress to product/ in the URL, infact, opening (i.e.) this URL it works:
http://localhost:1234/my-site/product/?url=product-number-one
^
product.php
but I need to rewrite it to this
http://localhost:1234/my-site/product/product-number-one
.htaccess on Wordpress is by default this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /my-site/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /my-site/index.php [L]
</IfModule>
# END WordPress
I added to that this rule:
RewriteRule ^(.*) product?url=$1 [R]
but doesn't work. I tried others solutions but nothing done.
What you can do is very simple:
RewriteRule ^my-site/product/([A-Za-z0-9-]+)$ /my-site/filename_of_product_page.php?url=$1 [L]
With the expression listed within the parentheses, you would need to be sure your url itself matches this- so use a scrubber function when inserting the name into the database.

Wordpress after migration doesn't work

I had problems with WordPress migration from one server to another, and generally now it works but I have many smaller problems..
Before I use domain oldexample.com and now i use domain newexample.com/something . Generally everything on the page works but changing language isn't work (qTranslate plugin).
I think that I found the reason of this - in admin menu I found in some places situation that href links start from "/" for example: "/wp-admin/..." and in result it change the URL from newexample.com/something/wp-admin to newexample.com/wp-admin. I see this problem in qTranslate settings links and when I want delete some plugins. I get the error message "404 Not Found - nginx/1.4.5" in results...
Did you see this problem before? Maybe I should change something in WordPress core files? .htaccess? Now it looks like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?newexample\.com\/something$ [NC]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
After long time I find the solution! All "href" are generated by using $_SERVER['REQUEST_URI'], and this variable show only the rest part of the path (without /something/. I add to wp-settings.php this part of code: $_SERVER['REQUEST_URI'] = '/something'.$_SERVER['REQUEST_URI']; (on the very beggining) all forms generate good path.

Retrieving GET[] information, from URL forwarding using htaccess

I have an index.php and news.php and .htaccess files in my localhost/DIRECTORY/AID/
folder, and I am basically trying send/receive data from index.php to news.php
This is a function inside the index.php, which creates a link from database query, and echos out a title of an article.
function news_preview() {
$query = "SELECT * FROM updates ORDER BY update_id DESC LIMIT 5 ";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$url = "/news/$row[update_id]/" . preg_replace('/[^a-zA-Z0-9-_]/', '_',
$row['update_title']);
echo " " . substr($row['update_title'], 0, 26) . "...<br/>";
}
}
echo news_preview();
Now, here is what the .htaccess looks like
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^news/([0-9]+)/([A-Za-z0-9_-]+)$ DIRECTORY/AID/news.php?id=$1 [QSA,L]
Now, to the problem. Basically, when I clicked on the link (generated by news_preview() )
shown in the index.php, All I get in the news.php page is nothing. But, probably because I am trying to use the $_GET['title'] Although, I am not certain if that is how we retrieve data. But, the links take me to http://localhost/news/46/This_is_news_title
which is perfect, but I am getting the Object Not Found error
Below, is the image of the error I am getting.
Put this code in the htdocs/.htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^news/([0-9]+)/.*$ /DIRECTORY/AID/news.php?id=$1 [QSA,L,NC]
In the AID/ folder along with index.php & news.php The problem, is I don't know how to get the data from the url in the news.php
The htaccess file needs to be in your document root. When the request URI is in the form:
/news/1234/something-something
The order apache uses to resolve whether overrides (i.e. stuff in htaccess files) should be applied is first see if this is a directory /news/1234/something-something and if so, if there's an htaccess file in it. That's not a directory so apache moves on. If /news/1234 is a directory, and if so, see if there's an htaccess file in it; since it's not, nothing happens. Then apache checks if /news is a directory and if so, check for htaccess; it's also not a directory so nothing happens. Finally, apache checks the document root / to see if there's an htaccess. Since the document root is a directory, that's where you need to put your rules.
The /DIRECTORY/AID/ directory is never in the mix here, unless that is actually where your document root is. If DIRECTORY/AID/ is your document root, e.g. the URI / maps directly to that directory, then you need to change your rules to:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^news/([0-9]+)/([A-Za-z0-9_-]+)$ news.php?id=$1 [QSA,L]
Sounds like you're expecting to be able to read the last portion of the URL as $_GET['title'], but your htaccess rule isn't adding it to the query string.
Try changing
RewriteRule ^news/([0-9]+)/([A-Za-z0-9_-]+)$ DIRECTORY/AID/news.php?id=$1 [QSA,L]
to
RewriteRule ^news/([0-9]+)/([A-Za-z0-9_-]+)$ DIRECTORY/AID/news.php?id=$1&title=$2 [QSA,L]

Categories